Random Maze
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1046 Accepted Submission(s): 356
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.
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
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
AC代码:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <queue>
#include <ctime>
#include <algorithm>
#define ll __int64
using namespace std;
const int INF = 1000000000;
const int maxn = 1000;
struct Edge{
int u, v, cost, cap, flow, next;
}et[maxn * maxn];
int low[maxn], pre[maxn], dis[maxn], eh[maxn];
bool vis[maxn];
int in[maxn], out[maxn];
int s, t, num, n, m, ans, anscost, ss, tt;
void init(){
memset(eh, -1, sizeof(eh));
num = 0;
}
void add(int u, int v, int cost, int cap, int flow){
Edge e = {u, v, cost, cap, flow, eh[u]};
et[num] = e;
eh[u] = num++;
}
void addedge(int u, int v, int cost, int cap){
add(u, v, cost, cap, 0);
add(v, u, -cost, 0, 0);
}
bool spfa(){
queue<int> Q;
memset(pre, -1, sizeof(pre));
memset(low, 0, sizeof(low));
memset(vis, false, sizeof(vis));
fill(&dis[0], &dis[maxn], INF);
dis[s] = 0, low[s] = INF, vis[s] = true;
Q.push(s);
while(!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = false;
for(int i = eh[u]; i != -1; i = et[i].next)
{
int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;
if(cap - flow && dis[v] > dis[u] + cost)
{
dis[v] = dis[u] + cost;
pre[v] = i;
low[v] = min(low[u], cap - flow);
if(!vis[v])
{
vis[v] = true;
Q.push(v);
}
}
}
}
return dis[t] != INF;
}
void costflow(){
ans = anscost = 0;
while(spfa())
{
int x = pre[t];
anscost += low[t] * dis[t];
ans += low[t];
while(x != -1)
{
et[x].flow += low[t];
et[x^1].flow -= low[t];
x = pre[et[x].u];
}
}
}
int main()
{
int ttt, u, v, a, b, ca = 0;
scanf("%d", &ttt);
while(ttt--)
{
scanf("%d%d%d%d", &n, &m, &ss, &tt);
init();
s = 0, t = n + 1;
memset(in, 0, sizeof(in));
memset(out, 0, sizeof(out));
int sum = 0;
while(m--)
{
scanf("%d%d%d%d", &u, &v, &a, &b);
if(a <= b)
{
in[v]++;
out[u]++;
addedge(v, u, b - a, 1);
sum += a;
}
else
{
addedge(u, v, a - b, 1);
sum += b;
}
}
in[ss]++;
out[tt]++;
int tot = 0;
for(int i = 1; i <= n; i++)
{
if(in[i] > out[i])
{
addedge(s, i, 0, in[i] - out[i]);
tot += (in[i] - out[i]);
}
else addedge(i, t, 0, out[i] - in[i]);
}
costflow();
printf("Case %d: ", ++ca);
if(ans == tot) printf("%d\n", anscost + sum);
else printf("impossible\n");
}
return 0;
}