你不能把数据规模改大点吗= =我优化都不加都过了
1 #include <cstdio> 2 #define INF 2147483647 3 int n,m,ans,x,y,z,M; 4 int d[201],l[300],fir[201],nex[500],to[500],wei[500]; 5 int min(int a,int b){if(a<b) return a;else return b;} 6 void add(int x,int y,int z){to[++M]=y;wei[M]=z;nex[M]=fir[x];fir[x]=M;} 7 int dfs(int now,int flow) 8 { 9 if(now==n) 10 return flow; 11 int used=0; 12 for(int i=fir[now];i;i=nex[i]) 13 if(d[to[i]]==d[now]+1 && wei[i]) 14 { 15 int fl=dfs(to[i],min(flow-used,wei[i])); 16 wei[i]-=fl,wei[i^1]+=fl; 17 used+=fl; 18 } 19 return used; 20 } 21 bool bfs() 22 { 23 int h=0,t=1; 24 l[1]=1;d[1]=0; 25 for(int i=2;i<=n;i++) d[i]=-1; 26 while(h<t) 27 for(int i=fir[l[++h]];i;i=nex[i]) 28 if(wei[i] && (d[to[i]]==-1)) 29 l[++t]=to[i],d[l[t]]=d[l[h]]+1; 30 return d[n]+1; 31 } 32 int main() 33 { 34 while(~scanf("%d%d",&m,&n)) 35 { 36 for(int i=1;i<=n;i++) 37 fir[i]=0; 38 for(ans=0,M=1;m;m--) 39 scanf("%d%d%d",&x,&y,&z),add(x,y,z),add(y,x,0); 40 while(bfs()) ans+=dfs(1,INF); 41 printf("%d ",ans); 42 } 43 return 0; 44 }
丧病的多组数据,一开始没看见