标签:容量 test key memory sts sed des mil plm
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18150 | Accepted: 7023 |
Description
Input
Output
Sample Input
4 5
1 2 1
2 3 1
3 4 1
1 3 2
2 4 2
Sample Output
6
Source
1 //# include <bits/stdc++.h> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 #include <queue> 6 using namespace std; 7 # define eps 1e-8 8 # define INF 0x3f3f3f3f 9 # define pi acos(-1.0) 10 # define MXN 1005 11 # define MXM 20050 12 13 struct Edge{ 14 int from, to, flow, cost, cap; 15 }edges[MXM*2]; //有向边数*2 16 17 struct MCMF{ 18 int n, m, idx; //点,边,边数 19 int flow, cost; 20 vector<int> G[MXN]; //记录边 21 int inq[MXN]; //BFS用 22 int dis[MXN]; //层次 23 int pre[MXN]; //上一条弧 24 int adf[MXN]; //增量 25 26 void Init(){ 27 idx=0; 28 for (int i=0;i<=n+1;i++) G[i].clear(); //有附加点时要注意 29 } 30 31 void Addedge(int u,int v,int cost,int cap){ 32 edges[idx++] = (Edge){u,v,0,cost,cap}; 33 edges[idx++] = (Edge){v,u,0,-cost,0}; 34 G[u].push_back(idx-2); 35 G[v].push_back(idx-1); 36 } 37 38 int Bellman(int s, int t) 39 { 40 memset(dis,0x3f,sizeof(dis)); //有附加点时要注意 41 memset(inq,0,sizeof(inq)); 42 dis[s]=0, inq[s]=1, adf[s]=INF; 43 queue<int> Q; 44 Q.push(s); 45 while (!Q.empty()) 46 { 47 int u = Q.front(); Q.pop(); 48 inq[u]=0; 49 for (int i=0;i<(int)G[u].size();i++) 50 { 51 Edge &e = edges[G[u][i]]; 52 if (dis[e.to] > dis[u] + e.cost && e.cap > e.flow) 53 { 54 dis[e.to] = dis[u] + e.cost; 55 adf[e.to] = min(adf[u], e.cap-e.flow); 56 pre[e.to] = G[u][i]; 57 if (!inq[e.to]) 58 { 59 Q.push(e.to); 60 inq[e.to]=1; 61 } 62 } 63 } 64 } 65 if (dis[t]==INF) return false; 66 67 flow+=adf[t]; 68 cost+=adf[t]*dis[t]; 69 int x=t; 70 while(x!=s) 71 { 72 edges[pre[x]].flow+=adf[t]; 73 edges[pre[x]^1].flow-=adf[t]; 74 x=edges[pre[x]].from; 75 } 76 return true; 77 } 78 79 int MinCost(int s,int t) 80 { 81 flow = 0, cost = 0; 82 while(Bellman(s, t)); 83 return cost; 84 } 85 }F; 86 87 int main() 88 { 89 scanf("%d%d",&F.n,&F.m); 90 F.Init(); 91 for (int i=1;i<=F.m;i++) 92 { 93 int u,v,cost; 94 scanf("%d%d%d",&u,&v,&cost); 95 F.Addedge(u,v,cost,1); 96 F.Addedge(v,u,cost,1); 97 } 98 F.Addedge(0, 1, 0, 2); 99 F.Addedge(F.n, F.n+1, 0, 2); 100 printf("%d\n",F.MinCost(0,F.n+1)); 101 return 0; 102 }
标签:容量 test key memory sts sed des mil plm
原文地址:http://www.cnblogs.com/haoabcd2010/p/7801065.html