The experience of having a previous problem with a template problem is easy.
Except because SPFA is not very familiar with the wrong two-letter and then debug a little
#include <iostream>#include<string>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<map>#include<vector>#include<Set>#include<algorithm>#defineINF 0x3f3f3f3fusing namespacestd;intSize, head[1010], next[6010], point[6010], val[6010];voidinit () {size=0; memset (Head,-1,sizeofhead);}voidAddint from,intTo,intvalue) {Point[size]=to ; Val[size]=value; Next[size]= head[ from]; head[ from] = size++;}BOOLSPFA (intSintN) { intdis[1010], time[1010]; BOOLvis[1010]; memset (DIS,0x3f,sizeofdis); memset (Time,0,sizeofTime ); memset (Vis,false,sizeofvis); Queue<int>Q; Q.push (s); Dis[s]=0; Vis[s]=true; while(!Q.empty ()) { intU =Q.front (); Q.pop (); Vis[u]=false; for(inti = Head[u]; ~i; i =Next[i]) { intj =Point[i]; if(Dis[j] > Dis[u] +Val[i]) {Dis[j]= Dis[u] +Val[i]; if(!Vis[j]) { //printf ("dis[%d] =%d\n", J, Dis[j]);Q.push (j); VIS[J]=true; if(++time[j] > N)return true; } } } } return false;}intMain () {intF, N, M, W; scanf ("%d", &f); while(f--) {init (); scanf ("%d%d%d", &n, &m, &W); while(m--){ intA, B, value; scanf ("%d%d%d", &a, &b, &value); Add (A, B, value); Add (b, a, value); } while(w--){ int from, to, value; scanf ("%d%d%d", & from, &to, &value); Add ( from, To,0-value); } if(SPFA (1, n)) puts ("YES"); ElsePuts"NO"); } return 0;}
Kuangbin_shortf (POJ 3259)