P1053Easy sssp
描述
輸入數據給出一個有N(2 <= N <= 1,000)個節點,M(M <= 100,000)條邊的帶權有向圖.
要求你寫一個程序, 判斷這個有向圖中是否存在負權回路. 如果從一個點沿着某條路徑出發, 又回到了自己, 而且所經過的邊上的權和小於0, 就說這條路是一個負權回路.
如果存在負權回路, 只輸出一行-1;
如果不存在負權回路, 再求出一個點S(1 <= S <= N)到每個點的最短路的長度. 約定: S到S的距離為0, 如果S與這個點不連通, 則輸出NoPath.
格式
輸入格式
第一行: 點數N(2 <= N <= 1,000), 邊數M(M <= 100,000), 源點S(1 <= S <= N);
以下M行, 每行三個整數a, b, c表示點a, b(1 <= a, b <= N)之間連有一條邊, 權值為c(-1,000,000 <= c <= 1,000,000)
輸出格式
如果存在負權環, 只輸出一行-1, 否則按以下格式輸出
共N行, 第i行描述S點到點i的最短路:
如果S與i不連通, 輸出NoPath;
如果i = S, 輸出0;
其他情況輸出S到i的最短路的長度.
樣例輸入
6 8 1
1 3 4
1 2 6
3 4 -7
6 4 2
2 4 5
3 6 3
4 5 1
3 5 4
樣例輸出
0
6
4
-3
-2
7
題解:
本來只是一個簡單的spfa判負環,結果出題人故意從中作怪。卡時+各種奇葩數據。。。
第三個數據1單獨成一個連通塊,另一個連通塊內有負環
第二個數據1單獨成一個連通塊,另一個連通塊內無負環
只有以1為起點,rand為起點算兩次了
最后還被cin cout卡掉了
又去網上找scanf和printf的資料,太亂了索性抄了個讀入優化
代碼:

1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #define maxn 5000+100 7 #define maxm 100000+1000 8 #define inf 1000000000 9 using namespace std; 10 inline int read() 11 { 12 int x=0,f=1;char ch=getchar(); 13 while(ch<'0'||ch>'9'){if(ch='-')f=-1;ch=getchar();} 14 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 15 return x*f; 16 } 17 struct edge{int go,next,w;}e[2*maxm]; 18 int n,m,k,s,tot,q[maxn],d[maxn],c[maxn],head[maxn],d2[maxn]; 19 bool v[maxn]; 20 void insert(int x,int y,int z) 21 { 22 e[++tot].go=y;e[tot].w=z;e[tot].next=head[x];head[x]=tot; 23 } 24 bool spfa() 25 { 26 for(int i=1;i<=n;++i) d[i]=inf; 27 memset(v,0,sizeof(v)); 28 int l=0,r=1,x,y;q[1]=s;d[s]=0;c[s]=0; 29 while(l!=r) 30 { 31 //cout<<l<<' '<<r<<endl; 32 x=q[++l];if(l==maxn)l=0;v[x]=0; 33 for(int i=head[x];i;i=e[i].next) 34 if(d[x]+e[i].w<d[y=e[i].go]) 35 { 36 d[y]=d[x]+e[i].w; 37 c[y]=c[x]+1;if(c[y]>n)return 0; 38 if(!v[y]){v[y]=1;q[++r]=y;if(r==maxn)r=0;} 39 } 40 } 41 return 1; 42 } 43 int main() 44 { 45 freopen("input.txt","r",stdin); 46 freopen("output.txt","w",stdout); 47 n=read();m=read();s=read();int x,y,z; 48 for(int i=1;i<=m;i++){x=read();y=read();z=read();insert(x,y,z);} 49 if(!spfa())printf("-1\n");else 50 { 51 for(int i=1;i<=n;i++)d2[i]=d[i]; 52 s=rand(); 53 if(!spfa()){printf("-1\n");return 0;}; 54 for(int i=1;i<=n;i++)if(d2[i]!=inf)printf("%d\n",d2[i]);else printf("NoPath\n"); 55 } 56 return 0; 57 }