hdu 4067 Random Maze(最小费用流)

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

解题报告见:http://www.cppblog.com/y346491470/articles/157734.html写得很好
http://www.starvae.com/?p=288

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 10005;
int n, m, s, t, flow, num, tot, sum, T;
int pre[maxn], dis[maxn], head[maxn];
bool vis[maxn];
int in[maxn], ou[maxn];
struct node
{
	int u, v, cap, flow, next, cost;
}edge[maxn];
void init()
{
	memset(in, 0, sizeof(in));
	memset(ou, 0, sizeof(ou));
	memset(head, -1, sizeof(head));
	tot = sum = 0;
}
void addedge(int u, int v, int cost, int cap)
{
	edge[tot].u = u;
	edge[tot].v = v;
	edge[tot].cap = cap;
	edge[tot].flow = 0;
	edge[tot].cost = cost;
	edge[tot].next = head[u];
	head[u] = tot++;

	edge[tot].u = v;
	edge[tot].v = u;
	edge[tot].cap = 0;
	edge[tot].flow = 0;
	edge[tot].cost = -cost;
	edge[tot].next = head[v];
	head[v] = tot++;
	return;
}
bool spfa()
{
	for (int i = 0; i <= t; i++)
	{
		dis[i] = inf;
		pre[i] = -1;
		vis[i] = false;
	}
	queue<int>q;
	dis[s] = 0;
	vis[s] = true;
	q.push(s);
	while (!q.empty())
	{
		int u, v;
		u = q.front();
		q.pop();
		vis[u] = false;
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			v = edge[i].v;
			if (edge[i].cap > edge[i].flow&&dis[v] > dis[u] + edge[i].cost)
			{
				dis[v] = dis[u] + edge[i].cost;
				pre[v] = i;
				if (!vis[v])
				{
					vis[v] = true;
					q.push(v);
				}
			}
		}
	}
	if (pre[t] == -1)
	{
		return false;
	}
	else
		return true;
}
int mincost()
{
	int ans = 0;
	while (spfa())
	{
		int _min = inf;
		for (int i = pre[t]; i != -1; i = pre[edge[i].u])
		{
			_min = min(_min, edge[i].cap);
		}
		for (int i = pre[t]; i != -1; i = pre[edge[i].u])
		{
			edge[i].cap-= _min;
			edge[i ^ 1].cap += _min;
		}
		ans += _min * dis[t];
		flow += _min;
	}
	return ans;
}
int main()
{
	 //freopen("C:/input.txt", "r", stdin);
	 int ss, tt;
	 int c = 0;
	 scanf("%d", &T);
	 while (T--)
	 {
		 init();
		 int tmp = 0;
		 flow = 0;
		 scanf("%d%d%d%d", &n, &m, &ss, &tt);
		 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, b - a, 1);
				 sum += a;
				 in[v]++;
				 ou[u]++;
			 }
			 else
			 {
				 addedge(u, v, a - b, 1);
				 sum += b;
			 }
		 }
		 in[ss]++;
		 ou[tt]++;
		 s = 0, t = n + 1;
		 for (int i = 1; i <= n; i++)
		 {
			 if (in[i] > ou[i])
			 {
				 addedge(s, i, 0, in[i] - ou[i]);
				 tmp += in[i] - ou[i];
			 }
			 else
			 {
				 addedge(i, t, 0, ou[i] - in[i]);
			 }
		 }
		 int cost = mincost();
		 printf("Case %d: ", ++c);
		 if (flow == tmp)
		 {
			 printf("%d\n", sum + cost);
		 }
		 else
		 {
			 printf("impossible\n");
		 }
	 }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Evildoer_llc/article/details/83247560