Tranvick

Min_cost_flow

Apr 8th, 2013
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:65777216")
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <vector>
  10. #include <set>
  11. #include <stack>
  12. #include <map>
  13. #include <queue>
  14. #include <string>
  15. #include <memory.h>
  16. #include <iterator>
  17. #define y1 trololoy1
  18. #define y0 trololoy0
  19. #define mem(A,X) memset(A,X,sizeof(A))
  20. #define memo(A) memset(A,0,sizeof(A))
  21. #define forn(I,B) for (int I=1;I<=(B);I++)
  22. #define forg(H,V) for (int H=first[V];h;h=next[H])
  23. #define rep(I,B) for (int I=0;I<(B);I++)
  24. #define labs(X) (((X)>0)?(X):(-(X)))
  25. #define ropen(X) freopen(X,"r",stdin)
  26. #define wopen(X) freopen(X,"w",stdout)
  27. #define rwopen(X) freopen(X".in","r",stdin);freopen(X".out","w",stdout)
  28. #define pb push_back
  29. #define mp make_pair
  30. #define all(X) (X).begin(),(X).end()
  31. #define sqr(X) ((X)*(X))
  32.  
  33. using namespace std;
  34.  
  35. typedef pair <int,int> pii;
  36. typedef double ld;
  37. typedef long long ll;
  38. typedef pair <ll,ll> pll;
  39. typedef vector<int> vi;
  40. const int N=66;
  41. const int M=22222;
  42. const int INF=111111111;
  43. const double eps=1e-9;
  44. const double pi=3.14159265358979;
  45.  
  46. int ef[M],es[M],first[N],next[M],ev[M],ec[M],S=1,T,a[N],b[N],x,y,z,c,n,sum1,sum2,ans,from[N],edge[N],d[N],m,k;
  47.  
  48. inline int be(int x){
  49.     return (x&1)?x+1:x-1;
  50. }
  51.  
  52. inline void add(int x,int y,int z,int co){
  53.     next[++c]=first[x];first[x]=c;
  54.     ef[c]=x;es[c]=y;ev[c]=z;ec[c]=co;
  55. }
  56.  
  57. void create_graph(){
  58.     rep(i,m){
  59.         int x,y,z;
  60.         scanf("%d%d%d",&x,&y,&z);
  61.         ++x;++y;
  62.         add(x,y,1,z);
  63.         add(y,x,0,-z);
  64.     }
  65.     T=n;
  66. }              
  67.  
  68. inline bool find_path(){
  69.     mem(d,63);
  70.     d[S]=0;
  71.     forn(i,n) forn(j,c){
  72.         if (!ev[j]) continue;
  73.         if (d[es[j]]>d[ef[j]]+ec[j] && d[ef[j]]<1000000000){
  74.             d[es[j]]=d[ef[j]]+ec[j];
  75.             from[es[j]]=ef[j];
  76.             edge[es[j]]=j;
  77.         }
  78.     }
  79.     return d[T]<1000000000;
  80. }
  81.  
  82. int main(){
  83.     scanf("%d%d%d",&n,&m,&k);
  84.     if (n==0 && m==0) return 0;
  85.     create_graph();
  86.     while (find_path()){
  87.         int q=INF;
  88.         for (int v=T;v!=S;v=from[v]) q=min(q,ev[edge[v]]);
  89.         ans+=min(k,q)*d[T];
  90.         k-=min(k,q);
  91.         if (k==0) break;
  92.         for (int v=T;v!=S;v=from[v]){
  93.             ev[edge[v]]-=q;
  94.             ev[be(edge[v])]+=q;
  95.         }
  96.     }
  97.     if (k) puts("Not possible");
  98.     else printf("%d\n",ans);
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment