hdu 4309 Seikimatsu Occult Tonneru(网络流,4级)

本文探讨了一种基于网络流算法解决战时人员疏散问题的方法。通过构建包含三种类型的路径(隧道、现代道路及古桥)的城市网络模型,旨在用最少的资源实现最多人员的安全疏散。文章提供了一个具体的算法实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Seikimatsu Occult Tonneru

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1357    Accepted Submission(s): 317


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

思路:网络流,源点到有人城市建边权为人口数,正常路建边权无穷,能藏人的路建无穷边,再建到汇点权为能藏的人数(最好拆点),最后枚举桥,修则权无穷否则为1

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int mm=5e3+9;
const int mn=2e3+9;
const int oo=1e9;
class knode
{
public:
    int next,flow,v;
} e[mm];
class znode
{
public:
    int u,v,w;
} f[3][mm];
int pos[3];
int q[mn],head[mn],work[mn],dis[mn],peo[mn];
int src,dest,node,edge,n,m;
void data(int _node,int _src,int _dest)
{
    node=_node;
    src=_src;
    dest=_dest;
    edge=0;
    memset(head,-1,sizeof(head));
}
void add(int u,int v,int c)
{
    e[edge].v=v;
    e[edge].flow=c;
    e[edge].next=head[u];
    head[u]=edge++;
    e[edge].v=u;
    e[edge].flow=0;
    e[edge].next=head[v];
    head[v]=edge++;
}
bool bfs()
{
    int l=0,r=1,u,v;
    for(int i=0; i<node; ++i)dis[i]=-1;
    q[l]=src;
    dis[src]=0;
    while(l^r)
    {
        u=q[l++];
        l%=mn;
        for(int i=head[u]; ~i; i=e[i].next)
        {
            v=e[i].v;
            if(e[i].flow&&dis[v]==-1)
            {
                dis[v]=dis[u]+1;
                q[r++]=v;
                r%=mn;
                if(v==dest)
                {
                    return 1;
                }
            }
        }
    }
    return 0;
}
int dfs(int u,int exp)
{
    if(u==dest)return exp;
    int tmp=0,v;
    for(int&i=work[u]; ~i; i=e[i].next)
    {
        v=e[i].v;
        if(e[i].flow&&dis[v]==dis[u]+1&&(tmp=dfs(v,min(e[i].flow,exp)))>0)
        {
            e[i].flow-=tmp;
            e[i^1].flow+=tmp;
            return tmp;
        }
    }
    return 0;
}

int dinic_flow()
{
    int ret=0,num=0;
    while(bfs())
    {
        for(int i=0; i<node; ++i)work[i]=head[i];
        while(num=dfs(src,oo))ret+=num;
    }
    return ret;
}
int main()
{
    int a,b,c,d,n,m;
    while(~scanf("%d%d",&n,&m))
    {
        pos[0]=pos[1]=pos[2]=0;
        for(int i=1; i<=n; ++i)
            scanf("%d",&peo[i]);
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d%d%d",&a,&b,&c,&d);
            if(d<0)f[2][pos[2]].u=a,f[2][pos[2]].v=b,f[2][pos[2]++].w=c;
            else if(d==0)f[1][pos[1]].u=a,f[1][pos[1]].v=b,f[1][pos[1]++].w=c;
            else f[0][pos[0]].u=a,f[0][pos[0]].v=b,f[0][pos[0]++].w=c;
        }
        int z=1<<pos[0];
        int cost=oo,cc,pe,people=0;
        for(int i=0; i<z; ++i)
        {
            cc=0;
            data(n+2+pos[2],0,n+1+pos[2]);
            for(int j=0; j<pos[0]; ++j)
            
                if(i&(1<<j))
                {
                    cc+=f[0][j].w;
                    add(f[0][j].u,f[0][j].v,oo);
                }
                else
                {
                    add(f[0][j].u,f[0][j].v,1);
                }
            for(int j=1; j<=n; ++j)
                if(peo[j])
                {
                    add(src,j,peo[j]);
                }
            for(int j=0; j<pos[1]; ++j)
                add(f[1][j].u,f[1][j].v,oo);
            for(int j=0; j<pos[2]; ++j)
            {
                add(f[2][j].u,n+j+1,oo),add(n+j+1,f[2][j].v,oo),add(n+j+1,dest,f[2][j].w);
            }
            pe=dinic_flow();
            if(pe>people)
            {
                people=pe;
                cost=cc;
            }
            else if(pe==people&&cc<cost)cost=cc;
        }
        if(people)printf("%d %d\n",people,cost);
        else puts("Poor Heaven Empire");
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值