HDU 4309 Seikimatsu Occult Tonneru(最大流SAP+状态压缩枚举)

Seikimatsu Occult Tonneru

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2012    Accepted Submission(s): 523

Problem Description

During the world war, to avoid the upcoming Carpet-bombing from The Third Reich, people in Heaven Empire went to Great Tunnels for sheltering.

There are N cities in Heaven Empire, where people live, with 3 kinds of directed edges connected with each other. The 1st kind of edges is one of Great Tunnels( no more than 20 tunnels) where a certain number of people can hide here; people can also go through
one tunnel from one city to another. The 2nd kind of edges is the so-called Modern Road, which can only let people go through. The 3rd kind of edges is called Ancient Bridge and all the edges of this kind have different names from others, each of which is
named with one of the twelve constellations( such as Libra, Leo and so on); as they were build so long time ago, they can be easily damaged by one person‘s pass. Well, for each bridge, you can spend a certain deal of money to fix it. Once repaired, the 3rd
kind of edges can let people pass without any limitation, namely, you can use one bridge to transport countless people. As for the former two kinds of edges, people can initially go through them without any limitation.

We want to shelter the most people with the least money.

Now please tell me the largest number of people who can hide in the Tunnels and the least money we need to spend to realize our objective.

Input

Multiple Cases.

The first line, two integers: N (N<=100), m (m<=1000). They stands for the number of cities and edges.

The next line, N integers, which represent the number of people in the N cities.

Then m lines, four intergers each: u, v, w, p (1<=u, v<=N, 0<=w<=50). A directed edge u to v, with p indicating the type of the edge: if it is a Tunnel then p < 0 and w means the maximum number people who can hide in the the tunnel; if p == 0 then it is a Modern
Road with w means nothing; otherwise it is an Ancient Bridge with w representing the cost of fixing the bridge. We promise there are no more than one edge from u to v.

Output

If nobody can hide in the Tunnels, print “Poor Heaven Empire”, else print two integers: maximum number and minimum cost.

Sample Input

4 4
2 1 1 0
1 2 0 0
1 3 0 0
2 4 1 -1
3 4 3 -1

4 4
2 1 1 0
1 2 0 0
1 3 3 1
2 4 1 -1
3 4 3 -1

Sample Output

4 0
4 3

Author

BUPT

Source

2012 Multi-University Training Contest 1

题意:

题意:给出一张N(N<=100)个点,M(M<=1000条)边的有向图。每个点上都有一些人。每条边有4个属性(u,v,w,p)。这些边分为三种:(1)p<0时,表示这条边是隧道,这条隧道从u连向v,虽然如果想通过这条隧道的话没有流量限制,但可以最多只容纳w人;(2)p=0时,这条边是道路,由u连向v,通过没有流量限制;(3)p>0时,表示这条边是古老的桥,u连向v,如果不修这座桥,则只能通过1人,但是如果花费w的费用修桥的话,则通过这座桥的流量便没有限制。桥的总数<12。求使得最多的人能够躲到隧道里时候的人数和在该情况下的最小费用。

解题:

先建源点S=0与每个有人的点建一条边,容量为人数。如果p<0表示的是隧道,需建两条边:( u , v , INF )  ,   ( u , T , w)。如果p==0则建:( u ,v , INF).

如果p>0则表示可能需要修的桥,记下每个桥在图的id号与修桥的花费,并建一条初始状态的边:( u ,v , 1)。建完图后,开始枚举修桥的状态,并修改对应边的容量,求最大流,求完一次,还原修改过的边容量为1。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define captype i   

const int MAXN = 100010;   //点的总数
const int MAXM = 400010;    //边的总数
const int INF = 1<<30;
struct EDG{
    int to,next;
    captype cap,flow;
} edg[MAXM];
int eid,head[MAXN];
int gap[MAXN];  //每种距离(或可认为是高度)点的个数
int dis[MAXN];  //每个点到终点eNode 的最短距离
int cur[MAXN];  //cur[u] 表示从u点出发可流经 cur[u] 号边  

void init(){
    eid=0;
    memset(head,-1,sizeof(head));
}
//有向边 三个参数,无向边4个参数
void addEdg(int u,int v,captype c,captype rc=0){
    edg[eid].to=v; edg[eid].next=head[u];
    edg[eid].cap=c; edg[eid].flow=0; head[u]=eid++;  

    edg[eid].to=u; edg[eid].next=head[v];
    edg[eid].cap=rc; edg[eid].flow=0; head[v]=eid++;
}
//预处理eNode点到所有点的最短距离
void BFS(int sNode, int eNode){
    queue<int>q;
    memset(gap,0,sizeof(gap));
    memset(dis,-1,sizeof(dis));
    gap[0]=1;
    dis[eNode]=0;
    q.push(eNode);
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=head[u]; i!=-1; i=edg[i].next){
            int v=edg[i].to;
            if(dis[v]==-1){
                dis[v]=dis[u]+1;
                gap[dis[v]]++;
                q.push(v);
            }
        }
    }
}
int S[MAXN];    //路径栈,存的是边的id号
captype maxFlow_sap(int sNode,int eNode, int n){  //注意:n为点的总个数,包括源点与汇点
    BFS(sNode, eNode);              //预处理eNode到所有点的最短距离
    if(dis[sNode]==-1) return 0;    //源点到不可到达汇点
    memcpy(cur,head,sizeof(head));  

    int top=0;  //栈顶
    captype ans=0;  //最大流
    int u=sNode;
    while(dis[sNode]<n){   //判断从sNode点有没有流向下一个相邻的点
        if(u==eNode){   //找到一条可增流的路
            captype Min=INF ;
            int inser;
            for(int i=0; i<top; i++)    //从这条可增流的路找到最多可增的流量Min
            if(Min>edg[S[i]].cap-edg[S[i]].flow){
                Min=edg[S[i]].cap-edg[S[i]].flow;
                inser=i;
            }
            for(int i=0; i<top; i++){
                edg[S[i]].flow+=Min;
                edg[S[i]^1].flow-=Min;  //可回流的边的流量
            }
            ans+=Min;
            top=inser;  //从这条可增流的路中的流量瓶颈 边的上一条边那里是可以再增流的,所以只从断流量瓶颈 边裁断
            u=edg[S[top]^1].to;  //流量瓶颈 边的起始点
            continue;
        }
        bool flag = false;  //判断能否从u点出发可往相邻点流
        int v;
        for(int i=cur[u]; i!=-1; i=edg[i].next){
            v=edg[i].to;
            if(edg[i].cap-edg[i].flow>0 && dis[u]==dis[v]+1){
                flag=true;
                cur[u]=i;
                break;
            }
        }
        if(flag){
            S[top++] = cur[u];  //加入一条边
            u=v;
            continue;
        }
        //如果上面没有找到一个可流的相邻点,则改变出发点u的距离(也可认为是高度)为相邻可流点的最小距离+1
        int Mind= n;
        for(int i=head[u]; i!=-1; i=edg[i].next)
        if(edg[i].cap-edg[i].flow>0 && Mind>dis[edg[i].to]){
            Mind=dis[edg[i].to];
            cur[u]=i;
        }
        gap[dis[u]]--;
        if(gap[dis[u]]==0) return ans;  //当dis[u]这种距离的点没有了,也就不可能从源点出发找到一条增广流路径
                                        //因为汇点到当前点的距离只有一种,那么从源点到汇点必然经过当前点,然而当前点又没能找到可流向的点,那么必然断流
        dis[u]=Mind+1;      //如果找到一个可流的相邻点,则距离为相邻点距离+1,如果找不到,则为n+1
        gap[dis[u]]++;
        if(u!=sNode) u=edg[S[--top]^1].to;  //退一条边  

    }
    return ans;
}  

struct Bridge{
    int eid,cost;
}fixEdg[15];
int main(){
    int n,m ,u,v,w,p;
    while(scanf("%d%d",&n,&m)>0){
        init();
        int s=0, t=n+1;
        for(int i=1;i<=n;i++){
            scanf("%d",&w);
            if(w) addEdg(s,i,w);    //源点与有人的点连接
        }
        int k=0;
        while(m--){
            scanf("%d%d%d%d",&u,&v,&w,&p);
            if(p<0){
                addEdg(u,v,INF);
                addEdg(u,t,w);
            }
            else if(p==0){
                addEdg(u,v,INF);
            }
            else{
                fixEdg[k].eid=eid;
                fixEdg[k++].cost=w;
                addEdg(u,v,1);
            }
        }
        int maxNum=0,minCost=0;
        for(int sta=0; sta<(1<<k); sta++){
            int cost=0;
            for(int i=0; (1<<i)<=sta; i++)
            if(sta&(1<<i)){
                cost+=fixEdg[i].cost;
                edg[fixEdg[i].eid].cap=INF;
            }

            for(int i=0; i<eid; i++)
                edg[i].flow=0;

            int ans = maxFlow_sap(s,t,t+1);
            if(ans>maxNum){
                maxNum=ans; minCost=cost;
            }
            else if(ans==maxNum&&cost<minCost)
                minCost=cost;

            for(int i=0; (1<<i)<=sta; i++)
            if(sta&(1<<i)){
                edg[fixEdg[i].eid].cap=1;
            }
        }
        if(maxNum==0)
            printf("Poor Heaven Empire\n");
        else
            printf("%d %d\n",maxNum,minCost);
    }
}
时间: 2024-10-05 13:10:25

HDU 4309 Seikimatsu Occult Tonneru(最大流SAP+状态压缩枚举)的相关文章

HDU 4309 Seikimatsu Occult Tonneru(网络流-最大流)

Seikimatsu Occult Tonneru Problem Description During the world war, to avoid the upcoming Carpet-bombing from The Third Reich, people in Heaven Empire went to Great Tunnels for sheltering. There are N cities in Heaven Empire, where people live, with

hdu 4309 Seikimatsu Occult Tonneru 枚举+最大流

http://blog.csdn.net/julyana_lin/article/details/8070949 题意: n个点,每个点有初始的值 ,三种 通道,1.隧道:可以用来躲避,有固定的容量,也可以用来传递.2.普通的道路,可以无限的通过.3.桥(最多有12座):不花费的话能通过一人,修之后可以无限通过.问最少花费最大可以隐藏人数. 解: 网络流 + 枚举 官方题解: 先不考虑可以修复的桥的性质,则可以将模型简化为n个点的人通过有通过人数上限的有向边,到达一些有人数上限的特殊的边(隧道)

HDU 4309 Seikimatsu Occult Tonneru

Seikimatsu Occult Tonneru Time Limit: 6000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 430964-bit integer IO format: %I64d      Java class name: Main During the world war, to avoid the upcoming Carpet-bombing from The Thi

HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)

http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道,隧道允许无限个人通过,并且可以容纳w个人:有桥,可以允许无限个人通过,但是不能容纳人:还有一些破桥,修复这些破桥需要w花费,如果不修复,那么最多只能通过一人,如果修复了,那么可以通过无限个人.求出在能安全到达隧道的最大人数时的最小代价.(上述都是单向边) 思路:出题人也是有心了..在题目中有说破桥的

HDU 4309 Seikimatsu Occult Tonneru 网络流+状压

题目链接:点击打开链接 题意: 题意:给出一张N(N<=100)个点,M(M<=1000条)边的有向图.每个点上都有一些人.每条边有4个属性(u,v,w,p).这些边分为三种:(1)p<0时,表示这条边是隧道,这条隧道从u连向v,虽然如果想通过这条隧道的话没有流量限制,但可以最多只容纳w人;(2)p=0时,这条边是道路,由u连向v,通过没有流量限制;(3)p>0时,表示这条边是古老的桥,u连向v,如果不修这座桥,则只能通过1人,但是如果花费w的费用修桥的话,则通过这座桥的流量便没有

HDU3605Escape(最大流SAP+状态压缩优化点的个数)

Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6239    Accepted Submission(s): 1474 Problem Description 2012 If this is the end of the world how to do? I do not know how. But now scient

HDU4309-Seikimatsu Occult Tonneru(最大流)

Seikimatsu Occult Tonneru Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1741    Accepted Submission(s): 438 Problem Description During the world war, to avoid the upcoming Carpet-bombing fro

HDU 5135 Little Zu Chongzhi&#39;s Triangles(状态压缩dp+Vector)

这道题是水题,当时直接贪心就过了. 多阶段决策,其实应该用dp,他人的代码使用Vector进行预处理. #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; int n, a[12]; double dp[1<<12]; double cal(int a, int b, i

ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u After trying hard for many years, Victor has finally received a pilot license. To have a celebration, he intends to buy himself an airplane and fl