Random Maze (hdu 4067 最小费用流 好题 贪心思想建图)

原创 2015年07月11日 08:57:46

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 
 


这一题建图很巧妙,自己想不出来,解题思路参考这位大神Here


代码:


#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 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;
}

//返回的是最大流,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;
                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;
}
/*
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 最小费用最大流

完全参考自: http://www.cnblogs.com/wally/archive/2013/08/29/3288898.html 建好图,套个模板就OK了。 code: #include us...

hdu 4067 Random Maze 最小费用最大流

题意: 给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费;否则,移除这条边需要花费b。              题目要求用最小费用构造一个有向图满足以下条件: ...

HDU4067 Random Maze 最小费用最大流 福州网络赛

Random Maze Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota...

hdu 4067 Random Maze(最小费用流)

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

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

#include #include #include #include #include #include #include #include #define inf 0x3f3f3f3f #def...

hdu 4067 Random Maze(最小费用最大流)

Random Maze Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T...
  • fp_hzq
  • fp_hzq
  • 2011年10月08日 16:05
  • 1635

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
  • CHCXCHC
  • 2014年10月28日 16:15
  • 425

【HDU】 4067 Random Maze 费用流

Random Maze Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot...

HDU 4067 Random Maze 费用流 构造欧拉通路

题目链接:点击打开链接 题意: 给定n个点m条有向边的图, 起点s ,终点t 下面m条边 u,v, a,b  若选择这条边花费为a, 不选择花费为b 构造一条欧拉通路使得起点是s,终点是t,且...

hdu 4067 Random Maze(费用流)

先贪心选边,然后根据平衡条件建图。
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Random Maze (hdu 4067 最小费用流 好题 贪心思想建图)
举报原因:
原因补充:

(最多只允许输入30个字)