ZJNU 1367 - Party-- senior

Find the shortest path from i to X, and from X to i

Can start running again positive figures from the shortest X, each point distance dis1 [i] as the X distance back to the point i

FIG pitted then reverse again from X, dis2 [i] as a distance from the i point from each point X,

Finally search dis1 [i] + dis2 [i] is the value of the maximum output

 1 /*
 2 Written By StelaYuri
 3 */
 4 #include<bits/stdc++.h>
 5 #define INF 0x3f3f3f3f
 6 using namespace std;
 7 typedef pair<short,int> P;
 8 vector<P> graph1[1005],graph2[1005];
 9 int N,M,X,dis1[1005],dis2[1005];
10 bool vis1[1005],vis2[1005];
11 queue<short> q;
12 int main()
13 {
14     ios::sync_with_stdio(0);cin.tie(0);
15     int i,j,d,cnt,len,ans=0;
16     short a,b,id;
17     cin>>N>>M>>X;
18     for(i=0;i<M;i++)
19     {
20         cin>>a>>b>>d;
21         graph1[a].push_back(P(b,d));
22         graph2[b].push_back(P(a,d));
23     }
24     memset(dis1,INF,sizeof dis1);
25     memset(dis2,INF,sizeof dis2);
26     memset(vis1,false,sizeof vis1);
27     memset(vis2,false,sizeof vis2);
28     dis1[X]=dis2[X]=0;
29     vis1[X]=vis2[X]=true;
30     q.push(X);
31     while(!q.empty())
32     {
33         id=q.front();
34         q.pop();
35         cnt=graph1[id].size();
36         for(i=0;i<cnt;i++)
37         {
38             len=dis1[id]+graph1[id][i].second;
39             if(!vis1[graph1[id][i].first]||len<dis1[graph1[id][i].first])
40             {
41                 dis1[graph1[id][i].first]=len;
42                 vis1[graph1[id][i].first]=true;
43                 q.push(graph1[id][i].first);
44             }
45         }
46     }
47     q.push(X);
48     while(!q.empty())
49     {
50         id=q.front();
51         q.pop();
52         cnt=graph2[id].size();
53         for(i=0;i<cnt;i++)
54         {
55             len=dis2[id]+graph2[id][i].second;
56             if(!vis2[graph2[id][i].first]||len<dis2[graph2[id][i].first])
57             {
58                 dis2[graph2[id][i].first]=len;
59                 vis2[graph2[id][i].first]=true;
60                 q.push(graph2[id][i].first);
61             }
62         }
63     }
64     for(i=1;i<=N;i++)
65     {
66         if(i==X)
67             continue;
68         ans=max(ans,dis1[i]+dis2[i]);
69     }
70     cout<<ans;
71     
72     return 0;
73 }

 

Guess you like

Origin www.cnblogs.com/stelayuri/p/12235257.html