HDU1535 spfa

题意 给定一张图,计算从1到各点的最短距离+各点到1的最短距离。

spfa即可,分别正向,反向建一个图。

水~

View Code
  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<stdlib.h>
  4 #include<queue>
  5 #include<algorithm>
  6 using namespace std;
  7 const int maxn = 1000005;
  8 const int maxm = 1000015;
  9 const int inf = 99999999;
 10 int cnt1,cnt2,head1[ maxn ],head2[ maxn ];
 11 struct node{
 12     int u,val,next;
 13 }edge1[ maxm ],edge2[ maxm ];
 14 int n,m;
 15 int dis[ maxn ],vis[ maxn ];
 16 void init(){
 17     cnt1=cnt2=0;
 18     memset( head1,-1,sizeof( head1 ));
 19     memset( head2,-1,sizeof( head2 ));
 20     //printf("%d\n",inf);
 21 }
 22 void addedge1( int a,int b,int c ){
 23     edge1[ cnt1 ].u=b;
 24     edge1[ cnt1 ].val=c;
 25     edge1[ cnt1 ].next=head1[ a ];
 26     head1[ a ]=cnt1++;
 27 }
 28 void addedge2( int a,int b,int c ){
 29     edge2[ cnt2 ].u=b;
 30     edge2[ cnt2 ].val=c;
 31     edge2[ cnt2 ].next=head2[ a ];
 32     head2[ a ]=cnt2++;
 33 }
 34 void spfa1( int s ){
 35     for( int i=1;i<=n;i++ ){
 36         dis[ i ]=inf;
 37         vis[ i ]=0;
 38     }
 39     queue<int>q;
 40     while( !q.empty() )
 41         q.pop();
 42     dis[ s ]=0;
 43     vis[ s ]=1;
 44     q.push( s );
 45     while( !q.empty() ){
 46         int now=q.front();
 47         q.pop();
 48         vis[ now ]=0;
 49         for( int i=head1[ now ];i!=-1;i=edge1[ i ].next ){
 50             int next=edge1[ i ].u;
 51             if( dis[ next ]>dis[ now ]+edge1[ i ].val ){
 52                 dis[ next ]=dis[ now ]+edge1[ i ].val;
 53                 if( vis[ next ]==0 ){
 54                     vis[ next ]=1;
 55                     q.push( next );
 56                 }
 57             }
 58         }
 59     }
 60     return ;
 61 }
 62 void spfa2( int s ){
 63     for( int i=1;i<=n;i++ ){
 64         dis[ i ]=inf;
 65         vis[ i ]=0;
 66     }
 67     queue<int>q;
 68     while( !q.empty() )
 69         q.pop();
 70     dis[ s ]=0;
 71     vis[ s ]=1;
 72     q.push( s );
 73     while( !q.empty() ){
 74         int now=q.front();
 75         q.pop();
 76         vis[ now ]=0;
 77         for( int i=head2[ now ];i!=-1;i=edge2[ i ].next ){
 78             int next=edge2[ i ].u;
 79             if( dis[ next ]>dis[ now ]+edge2[ i ].val ){
 80                 dis[ next ]=dis[ now ]+edge2[ i ].val;
 81                 if( vis[ next ]==0 ){
 82                     vis[ next ]=1;
 83                     q.push( next );
 84                 }
 85             }
 86         }
 87     }
 88     return ;
 89 }
 90 int main(){
 91     int ca;
 92     scanf("%d",&ca);
 93     while( ca-- ){
 94         init();
 95         scanf("%d%d",&n,&m);
 96         int a,b,c;
 97         while( m-- ){
 98             scanf("%d%d%d",&a,&b,&c);
 99             addedge1( a,b,c );
100             addedge2( b,a,c );
101         }
102         int ans=0;
103         spfa1( 1 );
104         //printf("spfa1:\n");
105         for( int i=2;i<=n;i++ ){
106             //printf("dis[%d]=%d \n",i,dis[i]);
107             ans+=dis[ i ];
108         }
109         spfa2( 1 );
110         //printf("spfa2:\n");
111         for( int i=2;i<=n;i++ ){
112         //    printf("dis[%d]=%d \n",i,dis[i]);
113             ans+=dis[ i ];
114         }
115         printf("%d\n",ans);
116     }
117     return 0;
118 }

 

 

posted @ 2013-02-24 22:31  xxx0624  阅读(594)  评论(0编辑  收藏  举报