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

题意:n座城市,每一个城市有ni个人,m条边, 三种建筑,分别为隧道。古桥,现代桥,隧道能够容纳一定数量的人,能够通过无数次。现代桥能够通过无数次,古桥假设不修善仅仅能通过一次,修缮后能够通过无数次,修缮须要一定的费用,且古桥的最大数量为12,如今每一个城市的人须要到隧道避难,问最多能避难的人数,以及最多人数的最小花费(古桥的修缮)。

思路:最大流。构造超级源点和汇点,每一个城市与源点连边流量为人数,现代桥能够通过无数次。连边,流量为无限。隧道能够通过无数次,连边流量为无限,与汇点连边。流量为能够容纳的人数。然后枚举古桥2^12修与不修的情况,做这么多遍最大流就可以。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100+10;
const int maxm = 1000+10;
const int inf = 1<<25;
int n,m,nume;
struct edge{
    int v,f,nxt;
};
struct build{
    int u,v,w;
    build(int u,int v,int w):u(u),v(v),w(w){}
};
vector<build> vb[3];
edge e[maxm];
int head[maxn];
int city[maxn];
void addedge(int u,int v,int c){
    e[++nume].nxt = head[u];
    e[nume].v = v;
    e[nume].f = c;
    head[u] = nume;
    e[++nume].nxt = head[v];
    e[nume].v = u;
    e[nume].f = 0;
    head[v] = nume;
}
void init(){
    memset(head,0,sizeof head);
    nume = 1;
}

queue<int> que;
bool vis[maxn];
int dist[maxn];
int src,sink;

void bfs(){
    memset(dist,0,sizeof dist);
    while(!que.empty()) que.pop();
    vis[src] = true;
    que.push(src);
    while(!que.empty()){
        int u = que.front();
        que.pop();
        for(int i = head[u]; i ; i = e[i].nxt){
            if(e[i].f && !vis[e[i].v]){
                que.push(e[i].v);
                vis[e[i].v] = 1;
                dist[e[i].v] = dist[u]+1;
            }
        }
    }
}

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

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

}
int main(){

    while(~scanf("%d%d",&n,&m)){
        src = 0;
        sink = n+1;
        for(int i = 0; i < 3; i++) vb[i].clear();
        for(int i = 1; i <= n; i++) scanf("%d",&city[i]);
        bool flag = 0;
        while(m--){
            int a,b,c,d;
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if(d==0){
                vb[1].push_back(build(a,b,c));//现代桥
            }
            else if(d<0){
                vb[0].push_back(build(a,b,c));//隧道
                flag = 1;
            }else{
                vb[2].push_back(build(a,b,c));//古代桥
            }
        }
        if(!flag){
            printf("Poor Heaven Empire\n");
            continue;
        }
        int d = vb[2].size();
        int maxf = -1,minc = inf;
        for(int i = 0; i < (1<<d); i++){
            init();
            int tc = 0;
            for(int j = 0; j < d; j++){
                if(i&(1<<j)){
                    addedge(vb[2][j].u,vb[2][j].v,inf);
                    tc += vb[2][j].w;
                }else{
                    addedge(vb[2][j].u,vb[2][j].v,1);
                }
            }
            for(int i = 1; i <= n; i++){
                addedge(0,i,city[i]);
            }
            for(int i = 0; i < vb[1].size(); i++){
                addedge(vb[1][i].u,vb[1][i].v,inf);
            }
            for(int i = 0; i < vb[0].size(); i++){
                addedge(vb[0][i].u,vb[0][i].v,inf);
                addedge(vb[0][i].u,n+1,vb[0][i].w);
            }
            int tm = maxflow();
            if(tm > maxf){
                maxf = tm;
                minc = tc;
            }
            if(tm==maxf && minc > tc){
                minc = tc;
            }

        }
        if(maxf==-1){
            printf("Poor Heaven Empire\n");
        }else{
            printf("%d %d\n",maxf,minc);
        }
    }
    return 0;
}
时间: 2024-08-06 07:59:11

HDU4309-Seikimatsu Occult Tonneru(最大流)的相关文章

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

hdu 最大流例题

1532 Drainage Ditches(入门)    [最大流]3549 Flow Problem(入门)    [最大流]3572 Task Schedule(基础)    [最大流]任务分配,判断满流2732 Leapin' Lizards(较难)    [最大流]3338 Kakuro Extension(较难,好题)    [最大流][数和]神奇最大流行进列出2883 kebab(中等)    [最大流]判断满流3605 Escape(中等,好题)    [最大流](可用多重匹配)4

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

对IO流的操作(文件大小,拷贝,移动,删除)

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.SequenceInputStream; class LjyFileClass { /*LjyFileClass工具类使用需知: * * 1.计算