HDU4067 Random Maze 最小费用最大流 福州网络赛

 

Random Maze

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 243 Accepted Submission(s): 63


Problem Description
In the game “A Chinese Ghost Story”, there are many random mazes which have some characteristic:
1.There is only one entrance and one exit.
2.All the road in the maze are unidirectional.
3.For the entrance, its out-degree = its in-degree + 1.
4.For the exit, its in-degree = its out-degree + 1.
5.For other node except entrance and exit, its out-degree = its in-degree.
HDU4067 Random Maze 最小费用最大流 福州网络赛_第1张图片
There is an directed graph, your task is removing some edge so that it becomes a random maze. For every edge in the graph, there are two values a and b, if you remove the edge, you should cost b, otherwise cost a.
Now, give you the information of the graph, your task if tell me the minimum cost should pay to make it becomes a random maze.


Input
The first line of the input file is a single integer T.
The rest of the test file contains T blocks.
For each test case, there is a line with four integers, n, m, s and t, means that there are n nodes and m edges, s is the entrance's index, and t is the exit's index. Then m lines follow, each line consists of four integers, u, v, a and b, means that there is an edge from u to v.
2<=n<=100, 1<=m<=2000, 1<=s, t<=n, s != t. 1<=u, v<=n. 1<=a, b<=100000

Output
For each case, if it is impossible to work out the random maze, just output the word “impossible”, otherwise output the minimum cost.(as shown in the sample output)

Sample Input
2
2 1 1 2
2 1 2 3
5 6 1 4
1 2 3 1
2 5 4 5
5 3 2 3
3 2 6 7
2 4 7 6
3 4 10 5
Sample Output
Case 1: impossible
Case 2: 27

Source
The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest

Recommend
lcy
 
解题报告见: http://www.cppblog.com/y346491470/articles/157734.html 写得很好
http://www.starvae.com/?p=288
 
代码:
#include<cstdio>
#include<cstring>
#define N 105
#define inf 999999999
#define min(a,b) ((a)<(b)?(a):(b))

int n,m,s,t,num,flow;
int low[N],pre[N],adj[N],q[N];
struct edge
{
	int u,v,c,w,next;
	edge(){}
	edge(int uu,int vv,int ww,int cc,int n)
	{u=uu;v=vv;c=cc;w=ww;next=n;}
}e[8005];
void insert(int u,int v,int w,int c)
{
	e[num]=edge(u,v,w,c,adj[u]);
	adj[u]=num++;
	e[num]=edge(v,u,-w,0,adj[v]);
	adj[v]=num++;
}
int spfa()
{
	int i,x,f[N]={0},head=0,tail=0;
	q[++tail]=s;
	memset(low,0x3f,sizeof(low));
	pre[s]=-1;
	low[s]=0;
	while(head!=tail)
	{
		x=q[head=(head+1)%N];
		f[x]=0;
		for(i=adj[x];i!=-1;i=e[i].next)
			if(e[i].c&&low[e[i].v]>low[x]+e[i].w)
			{
				pre[e[i].v]=i;
				low[e[i].v]=low[x]+e[i].w;
				if(!f[e[i].v])
				{
					f[e[i].v]=1;
					q[tail=(tail+1)%N]=e[i].v;
				}
			}
	}
	return low[t]<inf;
}
int mincost()
{
	int ans=0;
	while(spfa())
	{
		int v,minflow=inf;
		for(v=pre[t];v!=-1;v=pre[e[v].u])
			minflow=min(minflow,e[v].c);
		for(v=pre[t];v!=-1;v=pre[e[v].u])
		{
			e[v].c-=minflow;
			e[v^1].c+=minflow;
		}
		ans+=minflow*low[t];
		flow+=minflow;
	}
	return ans;
}
int main()
{
	int u,v,a,b,T,ss,tt,c=0;
	scanf("%d",&T);
	while(T--)
	{
		int i,cost,sum=0,tmp=0,in[105]={0},out[105]={0};
		scanf("%d%d%d%d",&n,&m,&ss,&tt);
		num=flow=0;
		memset(adj,-1,sizeof(adj));
		while(m--)
		{
			scanf("%d%d%d%d",&u,&v,&a,&b);
			if(a<b)
			{
				insert(v,u,b-a,1);
				sum+=a;
				in[v]++;
				out[u]++;
			}
			else
			{
				insert(u,v,a-b,1);
				sum+=b;
			}
		}
		in[ss]++;
		out[tt]++;
		s=0;
		t=n+1;
		for(i=1;i<=n;i++)
		{
			if(in[i]>out[i])
			{
				insert(s,i,0,in[i]-out[i]);
				tmp+=in[i]-out[i];
			}
			else
				insert(i,t,0,out[i]-in[i]);
		}
		cost=mincost();
		printf("Case %d: ",++c);
		if(flow==tmp)
			printf("%d\n",sum+cost);
		else
			puts("impossible");
	}
}

你可能感兴趣的:(网络,Random,Graph,insert,each,output)