简单题
1 /* 2 简单的bfs 3 */ 4 #include<algorithm> 5 #include<iostream> 6 #include<string.h> 7 #include<stdlib.h> 8 #include<stdio.h> 9 #include<math.h> 10 #include<queue> 11 #include<stack> 12 #include<map> 13 #include<set> 14 using namespace std; 15 typedef long long int64; 16 //typedef __int64 int64; 17 typedef pair<int64,int64> PII; 18 #define MP(a,b) make_pair((a),(b)) 19 const int inf = 0x3f3f3f3f; 20 const double pi=acos(-1.0); 21 const int dx[]={1,-1,0,0}; 22 const int dy[]={0,0,1,-1}; 23 const double eps = 1e-8; 24 const int maxm = 1000005; 25 const int maxn = 1005; 26 struct Edge{ 27 int u,v,next,val; 28 }edge[ maxm*2 ]; 29 int cnt ,head[ maxn ]; 30 bool vis[ maxn ]; 31 int dis1[ maxn ],dis2[ maxn ],girl[ maxn ]; 32 //int dp[ maxn ]; 33 queue<int>q; 34 //int pre[ maxn ]; 35 void init(){ 36 cnt = 0; 37 memset( head,-1,sizeof( head ) ); 38 } 39 void addedge( int a,int b,int c ){ 40 edge[ cnt ].u = a; 41 edge[ cnt ].v = b; 42 edge[ cnt ].val = c; 43 edge[ cnt ].next = head[ a ]; 44 head[ a ] = cnt ++; 45 } 46 47 int spfa( int n ){ 48 while( !q.empty() ) 49 q.pop(); 50 //memset( pre,-1,sizeof( pre ) ); 51 memset( vis,false,sizeof( vis ) ); 52 memset( dis1,0x3f,sizeof( dis1 ) ); 53 memset( dis2,0x3f,sizeof( dis2 ) ); 54 vis[ 1 ] = true; 55 q.push( 1 ); 56 dis1[ 1 ] = 0; 57 dis2[ 1 ] = girl[ 1 ]; 58 //for( int i=head[1];i!=-1;i=edge[i].next ){ 59 // pre[ edge[i].v ] = 1; 60 //} 61 while( !q.empty() ){ 62 int cur = q.front(); 63 q.pop(); 64 vis[ cur ] = false; 65 for( int i=head[cur];i!=-1;i=edge[i].next ){ 66 int nxt = edge[ i ].v; 67 if( dis1[nxt]>dis1[cur]+edge[i].val ){ 68 dis1[nxt] = dis1[cur]+edge[i].val; 69 //pre[ nxt ] = cur; 70 dis2[nxt] = dis2[cur]+girl[edge[i].v]; 71 if( !vis[nxt] ){ 72 vis[nxt] = true; 73 q.push( nxt ); 74 } 75 } 76 else if( dis1[nxt]==dis1[cur]+edge[i].val ){ 77 if( dis2[nxt]<dis2[cur]+girl[edge[i].v] ){ 78 //pre[ nxt ] = cur; 79 dis2[nxt]=dis2[cur]+girl[edge[i].v]; 80 if( !vis[nxt] ){ 81 vis[nxt] = true; 82 q.push( nxt ); 83 } 84 } 85 } 86 } 87 } 88 //printf("dis1[ %d ] = %d ",n,dis1[n]); 89 if( dis1[n]>=0x3f3f3f3f ) return -1; 90 else return dis2[ n ]; 91 /* 92 int ans = 0; 93 int cur = n; 94 while( 1 ){ 95 ans += girl[ cur ]; 96 cur = pre[ cur ]; 97 if( cur==-1 ){ 98 break; 99 } 100 } 101 return ans; 102 */ 103 } 104 105 int main(){ 106 int n; 107 while( scanf("%d",&n)==1 ){ 108 int m; 109 scanf("%d",&m); 110 for( int i=1;i<=n;i++ ) 111 scanf("%d",&girl[i]); 112 int a,b,c; 113 init(); 114 while( m-- ){ 115 scanf("%d%d%d",&a,&b,&c); 116 //if( a==b ) continue; 117 addedge( a,b,c ); 118 addedge( b,a,c ); 119 } 120 printf("%d ",spfa( n ) ); 121 } 122 return 0; 123 }