Random Maze
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
The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest
题意: 网上很多,我就不多说了;
题解: 这里我说下我建图的一些思想;@首先对于每一条边来说,我们需要贪心建图,不能直接说默认先建边,因为这样有可能会形成负环,导致陷入死循环,因为费用流没法处理负环问题。所以对于每条边来说,如果a>b,则删掉这条边,否则保留这条边。@接着需要统计每个节点的度数,如果点的入度小与出度,则源点往其连边,反之往汇点连边。然后就是连边需要注意的几点地方了。
@1》对于保留的边,我们在建边是需要反向建,因为当我们需要拆除这条边时,是基于点的出度大于入度,我们需要调整边,也即入度需要增加,反向建图表示入度加1
@2》对于拆除的边,我们在建边是需要正向建,因为党我们需要保留这条边是,是基于点的出度小于入度,我们需要增加出度,正向建边表示出度加1
@》最后就是需要在出口和入口之间建一条边
AC代码:
/* *********************************************** Author :xdlove Created Time :2015年09月03日 星期四 13时02分46秒 File Name :a.cpp ************************************************ */
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define REP_ab(i,a,b) for(int i = a; i <= b; i++)
#define REP(i, n) for(int i = 0; i < n; i++)
#define REP_1(i,n) for(int i = 1; i <= n; i++)
#define DEP(i,n) for(int i = n - 1; i >= 0; i--)
#define DEP_N(i,n) for(int i = n; i >= 1; i--)
#define CPY(A,B) memcpy(A,B,sizeof(B))
#define MEM(A) memset(A,0,sizeof(A))
#define MEM_1(A) memset(A,-1,sizeof(A))
#define MEM_INF(A) memset(A,0x3f,sizeof(A))
#define MEM_INFLL(A) memset(A,0x3f3f,sizeof(A))
#define mid ((l + r) >> 1)
#define lson (l, mid, u << 1)
#define rson (mid + 1, r, u << 1 | 1)
#define ls (u << 1)
#define rs (u << 1 | 1)
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 110;
const int MAXM = 1e5;
const int mod = 1e9 + 7;
struct DoubleQueue
{
int l,r,q[MAXN];
DoubleQueue()
{
l = r = 0;
}
bool empty()
{
return l == r;
}
void push_back(int v)
{
q[r++] = v;
r %= MAXN;
}
void push_front(int v)
{
l = (l - 1 + MAXN) % MAXN;
q[l] = v;
}
int front()
{
return q[l];
}
void pop_front()
{
l++;
l %= MAXN;
}
void pop_back()
{
r = (r - 1 + MAXN) % MAXN;
}
};
struct Edge
{
int to,next,cap,flow,cost;
}edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;
void init(int n)
{
N = n;
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
//cost *= -1;
// printf("u %d v %d cost %d cap %d\n",u,v,cost,cap);
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t)
{
DoubleQueue q;
for(int i = 0; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push_back(s);
while(!q.empty())
{
int u = q.front();
q.pop_front();
vis[u] = false;
for(int i = head[u]; ~i; i = edge[i].next)
{
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;
if(!vis[v])
{
vis[v] = true;
if(!q.empty() && dis[v] <= dis[q.front()])
q.push_front(v);
else q.push_back(v);
}
}
}
}
if(pre[t] == -1) return false;
return true;
}
int Minflow(int s,int t,int &cost)
{
int flow = 0;
cost = 0;
while(spfa(s,t))
{
//cout<<"sad"<<endl;
int Min = INF;
for(int i = pre[t]; ~i; i = pre[edge[i ^ 1].to])
{
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t]; ~i; i = pre[edge[i ^ 1].to])
{
edge[i].flow += Min;
edge[i ^ 1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
//cost *= -1;
return flow;
}
const string sb = "impossible";
int del[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T,cnt = 0;
cin>>T;
while(T--)
{
int n,m,s,t;
scanf("%d %d %d %d",&n,&m,&s,&t);
init(n + 2);
MEM(del);
int sum = 0;
for(int i = 1; i <= m; i++)
{
int u,v,a,b;
scanf("%d %d %d %d",&u,&v,&a,&b);
sum += min(a,b);
if(a <= b)
{
addedge(v,u,1,b - a);
del[u]--;
del[v]++;
}
else addedge(u,v,1,a - b);
}
int ss = 0,tt = n + 1;
int fuck = 0;
addedge(s,t,0,0);
del[t]--;
del[s]++;
for(int i = 1; i <= n; i++)
{
if(del[i] > 0)fuck += del[i];
if(del[i] > 0) addedge(ss,i,del[i],0);
if(del[i] < 0) addedge(i,tt,-del[i],0);
}
int cost;
int ans = Minflow(ss,tt,cost);
printf("Case %d: ",++cnt);
if(ans != fuck) puts("impossible");
else printf("%d\n",sum + cost);
//cout<<ans<<endl;
}
return 0;
}