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
 

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




转载于:https://www.cnblogs.com/toyking/p/3893145.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值