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

Recommend

zhuyuanchen520   |   We have carefully selected several similar problems for you:  4300 4301 4302 4303 4304

题目大意:

有n个城市,m个地道,接下来一行告诉你各个城市的初始人数,接下来m行介绍管道。

-1表示管道既可以经过又可以躲藏人。

0表示管道只能经过城市

1表示只能经过1次,再次经过需要花费建立,建立后就可以永久经过了。

解题思路:

根据样例二建立了如图所示的网络图,只需要枚举那个1号型号取与不取的01状态即可,枚举后求最大流。

解题代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

const int INF=(1<<30);
const int maxn=110,maxm=1100;

struct edge{
    int u,v,f,next;
    edge(int u0=0,int v0=0,int f0=0){
        u=u0;v=v0;f=f0;
    }
}e[4*maxm];

int src,sink,cnt,head[maxn];

void adde(int u,int v,int f){
    e[cnt].u=u,e[cnt].v=v,e[cnt].f=f,e[cnt].next=head[u],head[u]=cnt++;
    e[cnt].u=v,e[cnt].v=u,e[cnt].f=0,e[cnt].next=head[v],head[v]=cnt++;
}

void init(){
    cnt=0;
    memset(head,-1,sizeof(head));
}

queue <int> q;
bool visited[maxn];
int dist[maxn];

void bfs(){
    memset(dist,0,sizeof(dist));
    while(!q.empty()) q.pop();
    visited[src]=true;
    q.push(src);
    while(!q.empty()){
        int s=q.front();
        q.pop();
        for(int i=head[s];i!=-1;i=e[i].next){
            int d=e[i].v;
            if(e[i].f>0 && !visited[d]){
                q.push(d);
                dist[d]=dist[s]+1;
                visited[d]=true;
            }
        }
    }
}

int dfs(int u,int delta){
    if(u==sink) return delta;
    else{
        int ret=0;
        for(int i=head[u];delta && i!=-1;i=e[i].next){
            if(e[i].f>0 && dist[e[i].v]==dist[u]+1){
                int d=dfs(e[i].v,min(e[i].f,delta));
                e[i].f-=d;
                e[i^1].f+=d;
                delta-=d;
                ret+=d;
            }
        }
        return ret;
    }
}

int maxflow(){
    int ret=0;
    while(true){
        memset(visited,false,sizeof(visited));
        bfs();
        if(!visited[sink]) return ret;
        ret+=dfs(src,INF);
    }
    return ret;
}

int n,m;
vector <edge> qiao;

void initial(){
    qiao.clear();
    init();
    src=0;
    sink=n+1;
}

void input(){
    int u,v,w,id;
    for(int i=1;i<=n;i++){
        scanf("%d",&w);
        adde(src,i,w);
    }
    for(int i=1;i<=m;i++){
        scanf("%d%d%d%d",&u,&v,&w,&id);
        if(id==-1){
            adde(u,v,INF);
            adde(u,sink,w);
        }else if(id==0){
            adde(u,v,INF);
        }else{
            qiao.push_back(edge(u,v,w));
        }
    }
}

void solve(){
    int preflow=maxflow();
    pair <int,int> p=make_pair(preflow,0);
    int newhead[maxn];
    vector <edge> tmp;
    for(int i=0;i<cnt;i++) tmp.push_back(e[i]);
    for(int i=0;i<=n+1;i++) newhead[i]=head[i];

    for(int i=0;i<(1<<qiao.size());i++){
        int cost=0;
        for(int t=0;t<qiao.size();t++){
             if(i&(1<<t)){
                adde(qiao[t].u,qiao[t].v,INF);
                cost+=qiao[t].f;
             }else{
                adde(qiao[t].u,qiao[t].v,1);
             }
        }
        int tmpflow=maxflow()+preflow;
        if(tmpflow>p.first){
            p.first=tmpflow;
            p.second=cost;
        }
        else if(tmpflow==p.first) p.second=min(p.second,cost);

        cnt=tmp.size();
        for(int t=0;t<cnt;t++) e[t]=tmp[t];
        for(int t=0;t<=n+1;t++) head[t]=newhead[t];
    }
    if(p.first!=0) printf("%d %d\n",p.first,p.second);
    else printf("Poor Heaven Empire\n");
}

int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        initial();
        input();
        solve();
    }
    return 0;
}

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

时间: 2024-10-11 01:32:38

HDU 4309 Seikimatsu Occult Tonneru(网络流-最大流)的相关文章

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(最大流+二进制枚举)

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的费用修桥的话,则通过这座桥的流量便没有

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 fro

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

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 4289 Control(网络流 最大流+拆点)(模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1545    Accepted Submission(s): 677 Problem Description You, the head of Department o

HDU 3605Escape(缩点+网络流之最大流)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3605 本来打算昨天写两道题的,结果这个题卡住了,最后才发现是最后的判断条件出错了,判断满流的条件应该是与n的比较,竟然写成与所有星球总容量的比较了.(最近大脑短路..) 这题也不是完全自己想的,没想到缩点这一技巧,由于n的数据范围太大,普通的建图方法会超时超内存,需要缩点,因为对于每个点来说,一共只有2^10种方法,而最多一共有10W个点,显然有很多点是重复的,这时可以采取缩点的方法,将重复的当成一

[笔记] 网络流-最大流 POJ-1273\HDU-4240

[1] POJ-1273 题目:http://poj.org/problem?id=1273 最直接的最大流问题,用了Ford-Fulkerson方法,DFS随机搜索增广路. 算法原理参考:http://blog.csdn.net/smartxxyx/article/details/9293665 /************************ POJ-1273* Ford-Fulkerson***********************/#include <stdio.h> #inclu