好像有的朋友看不到題。在這裏貼一下題目。php
這道題就是USTC 的1280 這是題目連接,你們作出來能夠在這裏交代碼:點擊進入node
最短路徑的代價ios
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)編程
Total Submission(s) : 31 Accepted Submission(s) : 14網絡
Font: Times New Roman | Verdana | Georgiaspa
Font Size: ← →code
Problem Descriptionorm
Inputblog
Outputip
Sample Input
2 3 1 2 1 2 2 3 1 2 2 4 1 2 3 5 4 5 1 4 1 2 1 1 2 3 1 1 3 4 1 1 1 4 3 2 2 2 2 3 4 5 2 3 1 2 3 2 2 4 3 4 1 3 2 3 3 4 2 3 1 4 0 1 0 0
Sample Output
7 3 6
Source
#include <iostream> #include <algorithm> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <stack> //#pragma comment(linker, "/STACK:1024000000"); #define EPS (1e-8) #define unsignedBigInteger unsigned long long int #define _LL __int64 #define _INF 0x3f3f3f3f #define INF 1<<29 #define Mod 1000000007 #define BigInteger long long using namespace std; int min(int a,int b) { if(a>b)a=b; return a; } int max(int a,int b) { if(a<b)a=b; return a; } struct node { int to,w,c,next; } edge1[22005]; int head1[2005],dis[2005],deep[2005]; struct node edge3[22005]; int head3[2005],cnt3; int n,m,x,y,cnt1; bool vis[2005]; void addedge(int u,int v,int w,int c) { edge1[cnt1].to=v; edge1[cnt1].w=w; edge1[cnt1].c=c; edge1[cnt1].next=head1[u]; head1[u]=cnt1++; } void spfa(int ans) { queue <int>q; q.push(ans); vis[ans]=true; dis[ans]=0; while(!q.empty()) { int p,t=q.front(); q.pop(); p=head1[t]; vis[t]=false; while(p!=-1) { if(dis[edge1[p].to]>dis[t]+edge1[p].w) { dis[edge1[p].to]=dis[t]+edge1[p].w; if(!vis[edge1[p].to]) { vis[edge1[p].to]=true; q.push(edge1[p].to); } } p=edge1[p].next; } } } bool bfs(int x,int y) { memset(deep,-1,sizeof(deep)); queue <int > q; q.push(x); deep[x]=0; while(!q.empty()) { int t=q.front(); q.pop(); int p=head3[t]; while(p!=-1) { int v=edge3[p].to; if( deep[v]==-1&&edge3[p].w>0) { q.push(v); deep[v]=deep[t]+1; } p=edge3[p].next; } } return deep[y]!=-1; } int dfs(int src,int flow,int y) { if(src==y)return flow; int sum=0; int p=head3[src]; while(p!=-1) { int v=edge3[p].to; if(deep[v]==deep[src]+1&&edge3[p].w>0) { int tmp=dfs(v,min(flow-sum,edge3[p].w),y); sum+=tmp; edge3[p].w-=tmp; edge3[p^1].w+=tmp; if(flow-sum==0)break; } p=edge3[p].next; } return sum; } int dinic(int x,int y) { int ans=0; while(bfs(x,y)) { ans+=dfs(x,INF,y); } return ans; } void addedge3(int u,int v,int w) { edge3[cnt3].to=v; edge3[cnt3].w=w; edge3[cnt3].next=head3[u]; head3[u]=cnt3++; edge3[cnt3].to=u; edge3[cnt3].w=0; edge3[cnt3].next=head3[v]; head3[v]=cnt3++; } void bfs1(int x,int y) { memset(deep,0,sizeof(deep)); queue <int > q; q.push(x); deep[x]=1; while(!q.empty()) { int t=q.front(); q.pop(); if(t==y)continue; int p=head1[t]; while(p!=-1) { if(dis[edge1[p].to]-dis[t]==edge1[p].w) { int v=edge1[p].to; addedge3(t,v,edge1[p].c); if(deep[v]==0) { deep[v]=1; q.push(v); } } p=edge1[p].next; } } } int main() { int u,v,w,c,case1=1; while(scanf("%d%d",&n,&m),n||m) { scanf("%d%d",&x,&y); for(int i=0; i<=n; i++) { dis[i]=INF; vis[i]=false; head1[i]=-1; head3[i]=-1; } cnt1=0; cnt3=0; for(int i=0; i<m; i++) { scanf("%d%d%d%d",&u,&v,&w,&c); addedge(u,v,w,c); addedge(v,u,w,c); } spfa(x); //printf("%d\n",dis[y]); bfs1(x,y); printf("%d\n",dinic(x,y)); } return 0; }