题意: 给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费;否则,移除这条边需要花费b。
题目要求用最小费用构造一个有向图满足以下条件:
1.只有一个入口和出口
2.所有路都是唯一方向
3.对于入口s,它的出度 = 它的入度 + 1
4.对于出口t,它的入度 = 它的出度 + 1
5.除了s和t外,其他点的入度 = 其出度
最后如果可以构造,输出最小费用;否则输出impossible。
思路: 表示建图太神。。给个博客链接:http://www.cppblog.com/y346491470/articles/157734.html,详见代码:
/********************************************************* file name: hdu4067.cpp author : kereo create time: 2015年02月09日 星期一 15时01分42秒 *********************************************************/ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<set> #include<map> #include<vector> #include<stack> #include<cmath> #include<string> #include<algorithm> using namespace std; typedef long long ll; const int sigma_size=26; const int N=100+50; const int MAXN=2500+50; const int inf=0x3fffffff; const double eps=1e-8; const int mod=1000000000+7; #define L(x) (x<<1) #define R(x) (x<<1|1) #define PII pair<int, int> #define mk(x,y) make_pair((x),(y)) int n,m,edge_cnt,s,t,ans; int head[N],inq[N],d[N],pre[N],in[N],out[N],A[N]; struct Edge{ int v,cap,flow,cost,next; }edge[MAXN<<1]; void init(){ edge_cnt=0; memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); memset(head,-1,sizeof(head)); } void addedge(int u,int v,int cap,int cost){ edge[edge_cnt].v=v; edge[edge_cnt].cap=cap; edge[edge_cnt].flow=0; edge[edge_cnt].cost=cost; edge[edge_cnt].next=head[u]; head[u]=edge_cnt++; edge[edge_cnt].v=u; edge[edge_cnt].cap=0; edge[edge_cnt].flow=0; edge[edge_cnt].cost=-cost; edge[edge_cnt].next=head[v]; head[v]=edge_cnt++; } bool spfa(int st,int ed,int &flow,int &cost){ memset(inq,0,sizeof(inq)); for(int i=0;i<=ed;i++) d[i]=inf; d[st]=0; inq[st]=1; pre[st]=st; A[st]=inf; queue<int>Q; Q.push(st); while(!Q.empty()){ int u=Q.front(); Q.pop(); inq[u]=0; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].v,w=edge[i].cost; if(edge[i].cap>edge[i].flow && d[v]>d[u]+w){ d[v]=d[u]+w; pre[v]=i; A[v]=min(A[u],edge[i].cap-edge[i].flow); if(!inq[v]){ inq[v]=1; Q.push(v); } } } } if(d[ed] == inf) return false; flow+=A[ed]; cost+=A[ed]*d[ed]; int u=ed; while(u!=st){ edge[pre[u]].flow+=A[ed]; edge[pre[u]^1].flow-=A[ed]; u=edge[pre[u]^1].v; } return true; } int MinCostFlow(int st,int ed){ int flow=0,cost=0; while(spfa(st,ed,flow,cost)) ; ans=flow; return cost; } int main(){ //freopen("in.txt","r",stdin); int T,kase=0; scanf("%d",&T); while(T--){ printf("Case %d: ",++kase); init(); scanf("%d%d%d%d",&n,&m,&s,&t); int sum=0; for(int i=0;i<m;i++){ int u,v,a,b; scanf("%d%d%d%d",&u,&v,&a,&b); if(a<=b){ addedge(v,u,1,b-a); in[v]++; out[u]++; sum+=a; } else{ addedge(u,v,1,a-b); sum+=b; } } in[s]++; out[t]++; int cnt=0; for(int i=1;i<=n;i++){ cnt+=in[i]; addedge(0,i,in[i],0); addedge(i,n+1,out[i],0); } int res=MinCostFlow(0,n+1); if(cnt!=ans){ printf("impossible\n"); continue; } printf("%d\n",sum+res); } return 0; }
时间: 2024-08-09 10:44:25