D.Cow Party

oj加题的时候看能不能过


  1 #include<iostream>
  2 #include<string.h>
  3 #include<stdio.h>
  4 #include<queue>
  5 using namespace std;
  6 
  7 const int maxn=1005;
  8 const int INF=0xffffff;
  9 
 10 int G[maxn][maxn],a[maxn];
 11 int ma[maxn];
 12 
 13 queue<int>q;
 14 int vis[maxn];
 15 int n;
 16 
 17 int bfs(int x)
 18 {
 19     for(int i=1;i<=n;i++)
 20     {
 21         a[i]=INF;
 22     }
 23     while(!q.empty())
 24         q.pop();
 25     memset(vis,0,sizeof(vis));
 26     vis[x]=1;
 27     a[x]=0;
 28     q.push(x);
 29 
 30     while(!q.empty())
 31     {
 32         int tmp=q.front();
 33         q.pop();
 34         for(int i=1;i<=n;i++)
 35         {
 36             if(G[tmp][i])
 37             {
 38                 if(a[tmp]+G[tmp][i]<a[i]){
 39                 a[i]=a[tmp]+G[tmp][i];
 40                 q.push(i);
 41                 }
 42             }
 43         }
 44     }
 45 }
 46 
 47 int bfs2(int x)
 48 {
 49     for(int i=1;i<=n;i++)
 50     {
 51         ma[i]=INF;
 52     }
 53     while(!q.empty())
 54         q.pop();
 55     memset(vis,0,sizeof(vis));
 56     vis[x]=1;
 57     ma[x]=0;
 58     q.push(x);
 59 
 60     while(!q.empty())
 61     {
 62         int tmp=q.front();
 63         q.pop();
 64         for(int i=1;i<=n;i++)
 65         {
 66             if(G[i][tmp])
 67             {
 68                 if(ma[tmp]+G[i][tmp]<ma[i]){
 69                 ma[i]=ma[tmp]+G[i][tmp];
 70                 q.push(i);
 71                 }
 72             }
 73         }
 74     }
 75 }
 76 
 77 
 78 int main()
 79 {
 80     int m,x,aa,bb,t;
 81 
 82     while(cin>>n>>m>>x)
 83     {
 84         memset(G,0,sizeof(G));
 85 
 86         while(m--)
 87         {
 88             cin>>aa>>bb>>t;
 89             G[aa][bb]=t;
 90         }
 91 
 92 
 93         int sum=0;
 94         for(int i=1;i<=n;i++)
 95         {
 96             bfs(i);
 97             bfs2(i);
 98             cout<<a[x]<<" "<<ma[x]<<endl;
 99             if(sum<a[x]+ma[x])
100             sum=a[x]+ma[x];
101         }
102         cout<<sum<<endl;
103     }
104     return 0;
105 }

 

posted @ 2014-03-02 20:34  悠悠我心。  阅读(722)  评论(0编辑  收藏  举报