538人阅读 评论(0)

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
*/


1
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：153697次
• 积分：5519
• 等级：
• 排名：第4780名
• 原创：261篇
• 转载：3篇
• 译文：0篇
• 评论：43条
阅读排行
最新评论