ZJNU 1367 - Party--中高级

寻找从i到X,再从X到i的最短路

可以在正向图中从X开始跑一遍最短路,每个点的距离dis1[i]当作从X回到点i的距离

再将图反向从X再跑一遍,每个点的距离dis2[i]当作从i到点X的距离

最后搜索dis1[i]+dis2[i]值最大的输

 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 }

 

上一篇:ZJNU 1213 - 取水——高级


下一篇:纯css实现瀑布流