码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-05 19:22:20      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   for   ar   

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次,再次经过需要花费建立,建立后就可以永久经过了。


解题思路:

bubuko.com,布布扣

根据样例二建立了如图所示的网络图,只需要枚举那个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(网络流-最大流),布布扣,bubuko.com

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

标签:des   style   http   color   os   io   for   ar   

原文地址:http://blog.csdn.net/a1061747415/article/details/38387735

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!