70分蒟蒻求助

P1993 小 K 的农场

这个代码是40分的。。贴错了,请看这个。 ``` #include <cstdio> #include <iostream> using namespace std; const int inf=999999999; int n,m; int first[10000*5+10],next[10000*5+10],to[10000*5+10],val[10000*5+10]; int dis[10000*5+10]; int tot=1; int book[10000*5+10]; int read() { char ch=getchar(); int x=0,f=1; while(ch<'0' || ch>'9') { if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return f*x; } void add(int u,int v,int w) { tot++; next[tot]=first[u]; first[u]=tot; to[tot]=v; val[tot]=w; } int spfa(int now) { book[now]=1; for(int i=first[now];i;i=next[i]) { int x=to[i]; if(dis[x]>=dis[now]+val[i]) { dis[x]=dis[now]+val[i]; if(book[x]==1) return 0; if(!spfa(x)) return 0; } } book[now]=0; return 1; } int main() { int a,b,c; n=read(); m=read(); int type; for(int i=1;i<=m;i++) { type=read(); a=read(); b=read(); if(type==1) { c=read(); add(a,b,c); } if(type==2) { c=read(); add(b,a,c); } if(type==3) { add(a,b,0); add(b,a,0); } } for(int i=1;i<=n;i++) { add(i,0,0); dis[i]=inf; } if(spfa(0)==0) printf("No"); else printf("Yes"); return 0; }
by mendessy @ 2019-07-19 10:04:35


|