【luogu P1821 [USACO07FEB]银牛派对Silver Cow Party】 题解

题目链接:https://www.luogu.org/problemnew/show/P1821

反向多存一个图,暴力跑两遍

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <queue>
 5 #include <cstring>
 6 using namespace std;
 7 const int maxn = 100001;
 8 const int inf = 0x7fffffff;
 9 int n, m, x, dis1[maxn], dis2[maxn];
10 bool vis1[maxn] = {0}, vis2[maxn] = {0};
11 struct edge{
12     int len, to, next;
13 }e1[maxn], e2[maxn];
14 int head1[maxn], head2[maxn], cnt1, cnt2;
15 void SPFA1()
16 {
17     queue<int> q1;
18     vis1[x] = 1;
19     dis1[x] = 0;
20     q1.push(x);
21     while(!q1.empty())
22     {
23         int now1 = q1.front();
24         q1.pop();
25         vis1[now1] = 0;
26         for(int i = head1[now1]; i!=0 ; i = e1[i].next)
27         {
28             if(dis1[e1[i].to] > dis1[now1]+e1[i].len)
29             {
30                 dis1[e1[i].to] = dis1[now1]+e1[i].len;
31                 if(!vis1[e1[i].to])
32                 {
33                     vis1[e1[i].to] = 1;
34                     q1.push(e1[i].to);
35                 }
36             }
37         }
38     }
39 }
40 void SPFA2()
41 {
42     queue<int> q2;
43     vis2[x] = 1;
44     dis2[x] = 0;
45     q2.push(x);
46     while(!q2.empty())
47     {
48         int now2 = q2.front();
49         q2.pop();
50         vis2[now2] = 0;
51         for(int i = head2[now2]; i ; i = e2[i].next)
52         {
53             if(dis2[e2[i].to] > dis2[now2]+e2[i].len)
54             {
55                 dis2[e2[i].to] = dis2[now2]+e2[i].len;
56                 if(!vis2[e2[i].to])
57                 {
58                     vis2[e2[i].to] = 1;
59                     q2.push(e2[i].to);
60                 }
61             }
62         }
63     }
64 }
65 int main()
66 {
67     cin>>n>>m>>x;
68     for(int i = 1; i <= n; i++)
69     {
70         dis2[i] = inf;
71         dis1[i] = inf;
72     }
73     for(int i = 1; i <= m; i++)
74     {
75         int u,v,w;
76         cin>>u>>v>>w;
77         e1[i].to = v;
78         e1[i].len = w;
79         e1[i].next = head1[u];
80         head1[u] = i;
81 
82         e2[i].to = u;
83         e2[i].len = w;
84         e2[i].next = head2[v];
85         head2[v] = i;
86     }
87     SPFA1();
88     SPFA2();
89     int ans = 0;
90     for(int i = 1; i <= n; i++)
91     {
92         ans = max(dis1[i]+dis2[i],ans);
93     }
94     cout<<ans;
95     return 0;
96 }

Ctrl+C Ctrl+V 真毒瘤,弄得我12分反好几次,真是老年OI选手

posted @ 2018-04-25 21:56  Misaka_Azusa  阅读(159)  评论(0编辑  收藏  举报
Live2D