跳到主要內容

uva 10594








From Evernote:

uva 10594



最小費用最大流

這種題目好難寫.. 沒模板根本超累

[sourcecode language="cpp"]
//============================================================================
// Name : Data Flow.cpp
// Date : 2013 2013/3/1 下午6:03:09
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 105
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
int n,m,data,lc;
int q[M*M*M],rear,front;
struct node{
int u;
ll c,t;
}ntmp;
vector<node> mz[M];
void addpath(int x,int y,int c1,int t){
ntmp.u=y;
ntmp.c=c1;
ntmp.t=t;
mz[x].PB(ntmp);
ntmp.t=-t;
ntmp.c=0;
ntmp.u=x;
mz[y].PB(ntmp);
}
ll flow[M][M];
int father[M];
int fathere[M];
bool spfa(){
front=rear=0;
int vis[M],dis[M];
Set(vis,0);
fill(&dis[0],&dis[M-1],oo);
q[front++]=0;
vis[0]=1;
dis[0]=0;
while(front>rear){
int u=q[rear++];
int i=0;
FOR(it,mz[u]){
int v=(*it).u;
ll t=(*it).t,c=(*it).c;
if(dis[u]+t<dis[v]&&c>flow[u][v]){
dis[v]=dis[u]+t;
father[v]=u;
fathere[v]=i;
if(!vis[v]){
vis[v]=1;
q[front++]=v;
}
}
i++;
}
vis[u]=0;
}
if(dis[n]==oo)return false;
return true;
}
void maxflow(){
ll alltime=0,allflow=0;
Set(flow,0);
while(spfa()){
ll pflow=oo,time=0;
for(int i=n;i!=0;i=father[i]){
pflow=min(pflow,mz[father[i]][fathere[i]].c-flow[father[i]][i]);
time+=mz[father[i]][fathere[i]].t;
}
for(int i=n;i!=0;i=father[i]){
flow[father[i]][i]+=pflow;
flow[i][father[i]]-=pflow;
// printf("%d %d ",father[i],i);
}
// debug("\n");
alltime+=pflow*time;
allflow+=pflow;
// debug("%d %d\n",allflow,alltime);
}
if(allflow==data)printf("%lld\n",alltime);
else printf("Impossible.\n");
}
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%d%d",&n,&m)){
for(int i=0;i<=104;i++){
mz[i].clear();
}
int u[m],v[m],t[m];
for(int i=0;i<m;i++){
scanf("%d%d%d",&u[i],&v[i],&t[i]);
}
scanf("%d%d",&data,&lc);
for(int i=0;i<m;i++){
addpath(u[i],v[i],lc,t[i]);
addpath(v[i],u[i],lc,t[i]);
}
addpath(0,1,data,0);
maxflow();

}

}

[/sourcecode]

留言