# Random Maze

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

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   |   We have carefully selected several similar problems for you:  4064 4062 4063 4065 4066

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define mod 1000000009
#define INF 100000
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

const int MAXN = 105;
const int MAXM = 100000;

struct Edge
{
int to,next,cap,flow,cost;
}edge[MAXM];

int pre[MAXN],dis[MAXN],MIN[MAXN];
bool vis[MAXN];
int N;
int n,m,s,t;

int in[MAXN],out[MAXN];

void init(int n)
{
N=n;
tol=0;
}

void addedge(int u,int v,int cap,int cost)
{
edge[tol].to=v;
edge[tol].cap=cap;
edge[tol].cost=cost;
edge[tol].flow=0;
edge[tol].to=u;
edge[tol].cap=0;
edge[tol].cost=-cost;
edge[tol].flow=0;
}

bool spfa(int s,int t)
{
queue<int>q;
for (int i=0;i<N;i++)
{
dis[i]=INF;
vis[i]=false;
pre[i]=-1;
}
MIN[s]=INF;
dis[s]=0;
vis[s]=true;
q.push(s);
while (!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
{
int v=edge[i].to;
if (edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost)
{
dis[v]=dis[u] + edge[i].cost;
pre[v]=i;
MIN[v]=min(edge[i].cap-edge[i].flow,MIN[u]);
if (!vis[v])
{
vis[v]=true;
q.push(v);
}
}
}
}
return pre[t]!=-1;
}

//返回的是最大流，cost存的是最小费用
int minCostMaxflow(int s,int t,int &cost)
{
int flow=0;
cost=0;
while (spfa(s,t))
{
int Min=MIN[t];
for (int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
cost+=edge[i].cost*Min;
}
flow+=Min;
}
return flow;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
int i,j,T;
int u,v,a,b,sum,Case=1;
sf(T);
while (T--)
{
sum=0;
memset(in,0,sizeof(in));
scanf("%d%d%d%d",&n,&m,&s,&t);
init(n+2);
for (i=0;i<m;i++)
{
scanf("%d%d%d%d",&u,&v,&a,&b);
if (a<=b)
{
sum+=a;
--in[u];++in[v];
}
else
{
sum+=b;
//                in[v]++;in[u]--;
}
}
in[s]++;
in[t]--;
int tmp=0;
for (int i=1;i<=n;i++)
{
if (in[i]>0)
else
}
int x,ans;
x=minCostMaxflow(0,N-1,ans);
pf("Case %d: ", Case++);
if (x==tmp) printf("%d\n",ans+sum);
else printf("impossible\n");
}
return 0;
}
/*
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
*/


• 本文已收录于以下专栏：

## hdu 4067 Random Maze（最小费用流）

Random Maze Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T...
• WEYuLi
• 2014年01月13日 21:45
• 654

## HDU 5303 Delicious Apples (贪心 枚举 好题)

HDU 5303 Delicious Apples (贪心 枚举 好题)
• Tc_To_Top
• 2015年07月23日 23:50
• 1981

## HDU 4067(Random Maze) 最小费用流最大流

Random Maze Problem Description In the game “A Chinese Ghost Story”, there are many random mazes wh...
• ZSGG_ACM
• 2015年09月04日 19:08
• 582

## HDU 4067 Random Maze

• u013007900
• 2014年08月31日 15:33
• 509

## hdu 4067 Random Maze

/*   // 题目链接：http://acm.hdu.edu.cn/showproblem.php?pid=4067     解题报告人：SpringWater     题意描述： ...
• SprintfWater
• 2012年08月13日 20:51
• 1172

## 【HDU】5700 - 区间交（线段树 & 贪心）

• wyg1997
• 2016年08月13日 10:02
• 234

## 【HDU】 4067 Random Maze 费用流

Random Maze Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot...
• u013368721
• 2014年07月03日 19:16
• 557

## HDU 4067 Random Maze 费用流

//author: CHC //First Edit Time: 2014-10-16 10:05 //Last Edit Time: 2014-10-16 11:16 #include #incl...
• CHCXCHC
• 2014年10月28日 16:15
• 443

## HDU 4067 Random Maze（费用流）

• u013742332
• 2015年09月05日 21:05
• 424

## hdu 4067 Random Maze（费用流）

• No__stop
• 2014年07月25日 19:03
• 518

举报原因： 您举报文章：Random Maze (hdu 4067 最小费用流 好题 贪心思想建图) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)