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

本文探讨了如何利用网络流算法解决HDU4067 Random Maze问题。题目要求在限定的时间和内存限制内,通过构建和分析图来寻找最优路径。
摘要由CSDN通过智能技术生成
 

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.

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

Recommend
lcy
 
 
代码:
#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");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值