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 DescriptionIn the game "A Chinese Ghost Story", there is many random mazes which has some characteristic:
1.There is only one entrance and one exit.
2.All the road in the maze is 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 with its out-degree = its in-degree.
There is an directed graph, your task was removing some edge so, it becomes a random maze. For every edge in the graph, there is, both values A and B, if you remove the edge, you should cost B, otherwise cost a.
Now, give your information of the graph, your task if tell me the minimum cost should pay to make it becomes a random m Aze.
Inputthe first line of the input file was a single integer T.
The rest of the test file contains T blocks.
For each test case, there was a line with four integers, N, m, s and T, means that there be n nodes and m edges, S is the Entrance ' s index, and T is the exit's index. Then M. lines follow, each of the line consists of four integers, u, V, A and B, means that there are 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
Outputfor each case, if it was 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
22 1 1 22 1 2 35 6 1 41 2 3 12 5 4 55 3 2 33 2 6 72 4 7 63 4 10 5
Sample Output
Case 1:impossiblecase 2:27
Sourcethe 36th ACM/ICPC Asia Regional Fuzhou Site--online Contest
Recommendlcy | We have carefully selected several similar problems for you:4064 4062 4063 4065 4066
This problem is very ingenious, I can't think of it, the problem-solving ideas refer to this great God here
Code:
#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 L l;using namespace Std;const int maxn = 105;const int MAXM = 100000;struct edge{int to,next,cap,flow,cost;} Edge[maxm];int Head[maxn],tol;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; memset (head,-1,sizeof (Head));} 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].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) {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; for (int i=head[u];i!=-1;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; Min[v]=min (Edge[i].cap-edge[i].flow,min[u]); if (!vis[v]) {vis[v]=true; Q.push (v); }}}} return pre[t]!=-1;} The maximum flow is returned, the cost is the minimum charge 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; Addedge (V,U,1,B-A); --IN[U];++IN[V]; } else {sum+=b; Addedge (u,v,1,a-b);//in[v]++;in[u]--; }} in[s]++; in[t]--; int tmp=0; for (int i=1;i<=n;i++) {if (in[i]>0) Addedge (0,i,in[i],0); else Addedge (i,n-1,-in[i],0), tmp-=in[i]; } 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;} /*22 1 1 22 1 2 35 6 1 41 2 3 12 5 4 55 3 2 33 2 6 72 4 7 63 4 10 5*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Random Maze (hdu 4067 minimum cost flow good problem greedy idea building)