luoguP1821 [USACO07FEB] Silver Cow Party party Yinniu

Huashan Mountain, double experience

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int maxn=1e6+5;
  4 const int INF=1e9+7;
  5 struct A
  6 {
  7     int v,w,next;
  8 }e1[maxn],e2[maxn];
  9 int head1[maxn],head2[maxn],tot1,tot2;
 10 int n,m,k,d1[1005],d2[1005],v[1005];
 11 template <class t>void red(t &x)
 12 {
 13     x=0;
 14     int w=1;
 15     char ch=getchar();
 16     while(ch<'0'||ch>'9')
 17     {
 18         if(ch=='-')
 19             w=-1;
 20         ch=getchar();
 21     }
 22     while(ch>='0'&&ch<='9')
 23     {
 24         x=(x<<3)+(x<<1)+ch-'0';
 25         ch=getchar();
 26     }
 27     x*=w;
 28 }
 29 void input()
 30 {
 31     freopen("input.txt","r",stdin);
 32 }
 33 void add1(int x,int y,int z)
 34 {
 35     e1[++tot1].v=y;
 36     e1[tot1].w=z;
 37     e1[tot1].next=head1[x];
 38     head1[x]=tot1;
 39 }
 40 void add2(int x,int y,int z)
 41 {
 42     e2[++tot2].v=y;
 43     e2[tot2].w=z;
 44     e2[tot2].next=head2[x];
 45     head2[x]=tot2;
 46 }
 47 void read()
 48 {
 49     red(n);
 50     red(m);
 51     red(k);
 52     int x,y,z;
 53     for(int i=1;i<=m;++i)
 54     {
 55         red(x);
 56         red(y);
 57         red(z);
 58         add1(x,y,z);
 59         add2(y,x,z);
 60     }
 61 }
 62 queue<int>     q;
 63 void bfs1()
 64 {
 65     for(int i=1;i<=n;++i)
 66         for(int j=1;j<=n;++j)
 67             d1[i]=INF;
 68     d1[k]=0;        
 69     q.push(k);
 70     while(!q.empty())
 71     {
 72         int u=q.front();
 73         q.pop();
 74         v[u]=0;
 75         for(inthead1 i = [u] i; i = e1 [i] .next)
 76          {
 77              int v = e1 [i] .v;
78              int w = e1 [i] .w;
79              if (d1 [vi]> d1 [u] + w)
 80              {
 81                  d1 [vi] = d1 [u] + w;
82                  if (! V [v])
 83                  {
 84                      v [v] = 1 ;
85                      q.push (vi);
86                  }
 87              }
 88          }
 89      }
 90 }
 91 void bfs2()
 92 {
 93     for(int i=1;i<=n;++i)
 94         for(int j=1;j<=n;++j)
 95             d2[i]=INF;
 96     d2[k]=0;        
 97     q.push(k);
 98     while(!q.empty())
 99     {
100         int u=q.front();
101         q.pop();
102         v[u]=0;
103         for(int i=head2[u];i;i=e2[i].next)
104         {
105             int vi=e2[i].v;
106             int w=e2[i].w;
107             if(d2[vi]>d2[u]+w)
108             {
109                 d2[vi]=d2[u]+w;
110                 if(!v[vi])
111                 {
112                     v[vi]=1;
113                     q.push(vi);
114                 }
115             }
116         }
117     }
118 }
119 void work()
120 {
121     bfs1();    
122     bfs2();
123     int ans=0;
124     for(int i=1;i<=n;++i)
125         ans=max(ans,d1[i]+d2[i]);
126     printf("%d",ans);
127 }
128 int main()
129 {
130     //input();
131     read();
132     work();
133     return 0;
134 }
View Code

 

Guess you like

Origin www.cnblogs.com/Achensy/p/11008788.html