hdu 4067 Random Maze 最小费用最大流

题意: 给出n个点,m条边,入口s和出口t,对于每条边有两个值a,b,如果保留这条边需要花费;否则,移除这条边需要花费b。

题目要求用最小费用构造一个有向图满足以下条件:

1.只有一个入口和出口

2.所有路都是唯一方向

3.对于入口s,它的出度 = 它的入度 + 1

4.对于出口t,它的入度 = 它的出度 + 1

5.除了s和t外,其他点的入度 = 其出度

最后如果可以构造,输出最小费用;否则输出impossible。

思路: 表示建图太神。。给个博客链接:http://www.cppblog.com/y346491470/articles/157734.html,详见代码:

/*********************************************************
  file name: hdu4067.cpp
  author : kereo
  create time:  2015年02月09日 星期一 15时01分42秒
*********************************************************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int sigma_size=26;
const int N=100+50;
const int MAXN=2500+50;
const int inf=0x3fffffff;
const double eps=1e-8;
const int mod=1000000000+7;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define PII pair<int, int>
#define mk(x,y) make_pair((x),(y))
int n,m,edge_cnt,s,t,ans;
int head[N],inq[N],d[N],pre[N],in[N],out[N],A[N];
struct Edge{
    int v,cap,flow,cost,next;
}edge[MAXN<<1];
void init(){
    edge_cnt=0;
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost){
    edge[edge_cnt].v=v; edge[edge_cnt].cap=cap; edge[edge_cnt].flow=0;
    edge[edge_cnt].cost=cost; edge[edge_cnt].next=head[u]; head[u]=edge_cnt++;
    edge[edge_cnt].v=u; edge[edge_cnt].cap=0; edge[edge_cnt].flow=0;
    edge[edge_cnt].cost=-cost; edge[edge_cnt].next=head[v]; head[v]=edge_cnt++;
}
bool spfa(int st,int ed,int &flow,int &cost){
    memset(inq,0,sizeof(inq));
    for(int i=0;i<=ed;i++) d[i]=inf;
    d[st]=0; inq[st]=1; pre[st]=st; A[st]=inf;
    queue<int>Q;
    Q.push(st);
    while(!Q.empty()){
        int u=Q.front(); Q.pop();
        inq[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v,w=edge[i].cost;
            if(edge[i].cap>edge[i].flow && d[v]>d[u]+w){
                d[v]=d[u]+w; pre[v]=i;
                A[v]=min(A[u],edge[i].cap-edge[i].flow);
                if(!inq[v]){
                    inq[v]=1;
                    Q.push(v);
                }
            }
        }
    }
    if(d[ed] == inf)
        return false;
    flow+=A[ed]; cost+=A[ed]*d[ed];
    int u=ed;
    while(u!=st){
        edge[pre[u]].flow+=A[ed];
        edge[pre[u]^1].flow-=A[ed];
        u=edge[pre[u]^1].v;
    }
    return true;
}
int MinCostFlow(int st,int ed){
    int flow=0,cost=0;
    while(spfa(st,ed,flow,cost)) ;
    ans=flow;
    return cost;
}
int main(){
    //freopen("in.txt","r",stdin);
    int T,kase=0;
    scanf("%d",&T);
    while(T--){
        printf("Case %d: ",++kase);
        init();
        scanf("%d%d%d%d",&n,&m,&s,&t);
        int sum=0;
        for(int i=0;i<m;i++){
            int u,v,a,b;
            scanf("%d%d%d%d",&u,&v,&a,&b);
            if(a<=b){
                addedge(v,u,1,b-a);
                in[v]++; out[u]++; sum+=a;
            }
            else{
                addedge(u,v,1,a-b);
                sum+=b;
            }
        }
        in[s]++; out[t]++;
        int cnt=0;
        for(int i=1;i<=n;i++){
            cnt+=in[i];
            addedge(0,i,in[i],0);
            addedge(i,n+1,out[i],0);
        }
        int res=MinCostFlow(0,n+1);
        if(cnt!=ans){
            printf("impossible\n");
            continue;
        }
        printf("%d\n",sum+res);
    }
	return 0;
}
时间: 2024-08-09 10:44:25

hdu 4067 Random Maze 最小费用最大流的相关文章

HDU 4067 Random Maze 费用流

Random Maze Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1114    Accepted Submission(s): 387 Problem Description In the game "A Chinese Ghost Story", there are many random mazes which h

hdu 1533 Going Home 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need

hdu 3488(KM算法||最小费用最大流)

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 2925    Accepted Submission(s): 1407 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 30000

hdu 1853 Cyclic Tour 最小费用最大流

题意:一个有向图,现在问将图中的每一个点都划分到一个环中的最少代价(边权和). 思路:拆点,建二分图,跑最小费用最大流即可.若最大流为n,则说明是最大匹配为n,所有点都参与,每个点的入度和出度又是1,所以就是环. /********************************************************* file name: hdu1853.cpp author : kereo create time: 2015年02月16日 星期一 17时38分51秒 *******

hdu 1533 Going Home 最小费用最大流 入门题

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3125    Accepted Submission(s): 1590 Problem Description On a grid map there are n little men and n houses. In each unit time, every

HDU 1533--Going Home【最小费用最大流 &amp;&amp; 模板】

Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3452    Accepted Submission(s): 1771 Problem Description On a grid map there are n little men and n houses. In each unit time, every

HDU ACM 4494 Teamwork 最小费用最大流

题意:n个工作地,m种工人,工作地0是仓库,其他的都需要修缮,每个地点需要多个工种的工人若干,不同工种不能相互取代.每个工作地有一个开工时间,凑齐所有工人后准时开工,修缮也需要一定时间.一个工人可以在一个地方工作完后再到其他地方,两地直接的距离是欧几里得距离,可以算作时间.最少需要多少工人. 分析:只用费用流.每种工人不能相互替换,没有任何关系.因此对每个工种进行建图求解最小费用累加即可得到最终结果. 超级源点cs是仓库,超级汇点为ct. 一个地点拆成三个点,i.i'.i".k表示工种,对每个点

POJ 2195 &amp; HDU 1533 Going Home(最小费用最大流)

题目链接: POJ:http://poj.org/problem?id=2195 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1533 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically,

HDU 4067 Random Maze

题意: 一幅"随机图"定义为有如下性质的图: 有一个入口和一个出口 有向图 对于入口  出度比入度大1 对于出口  入度比出度大1 对于其他点  入度等于出度 现给出一幅有向图  每条边有2个决策--留下.扔掉  分别花费a和b  问  如果用最少的费用改造出"随机图" 思路: 网络流不错的题目  如果做过"混合图欧拉回路"(后文把这个问题成为p)那个zoj的题的话  这道题会有启发 来回忆p的做法  先将无向边随意定向  再利用度来将点划分成二