Hdu 4309Seikimatsu Occult Tonneru【最大流Dinic+暴力枚举Dfs+建图】人生难免磕磕碰碰

Seikimatsu Occult Tonneru

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


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
 





前言:


人生难免磕磕碰碰

从十点调试调到了1点,就因为输入的时候:

<0 >0 ==0是三种桥...而我在写代码的时候写成了==-1 ==1 ==0


结语:

退Acm退杭电保平安.....................


题目大意:

有n个城市,m条有向边,每个城市中的人数告诉你,并且告诉你这m条边都是什么类型的桥,问你最多能够藏起来的人数和对应的最小话费.

其中有三种桥:

1、第一种是隧道(<0),输入的四个元素里边,表示起点终点和能够藏匿的人数,最后一个元素表示当前种类。

2、第二种是现代大桥,输入的四个元素里边,表示起点终点和没有意义的一个元素,最后一个元素表示当前种类。

3、第三种是古代大桥,输入的四个严肃里边,表示起点终点和修复这个桥需要的花费,最后 一个元素表示当前种类。


隧道可以从起点走到终点,也可以将人藏匿起来。

现代大桥能够从起点走到终点。

古代大桥没修的话只能走过去一个人,如果修复了大桥,那么就可以走无限个人,就和现代大桥一样了。


思路:

1、问题突破点在于题干中的那句话,没有重边,并且古代大桥有12种星座来命名。


2、那么我们暴力枚举这些古桥是否维修,然后建图跑Dinic。


3、建图方式如下:

①从源点连接各个城市节点,其边权值为当前城市人口数,表示这个城市有多少个人。

②如果桥是隧道,那么对应连接两条有向边:从u到v,权值为INF,表示从u到v能走无限个人,另一条边从u到汇点T,权值为这个隧道能够藏匿的人数 ,表示如果走到了节点u,然后对应从u不过隧道,藏匿进去,其权值为能够藏匿的人数,表示这条隧道能够藏匿多少人。

如果桥是线代大桥,那么对应连接u,v,权值为INF,表示从u到v能走无限个人。

如果桥是古代大桥,如果维修了那么对应连接u,v,权值为INF,表示从u到v能走无限个人。否则那么对应连接u,v,权值为1,表示从u到v只能走一个人,走完桥就坏了,不能在走人了。


4、然后暴力跑Dinic,维护最大人数和最小花费。


Ac代码:

#include<stdio.h>
#include<queue>
#include<iostream>
#include<string.h>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int from;
    int to;
    int next;
    int w;
}e[100000];
struct qiao
{
    int x,y,op,w;
}a[100000];
int divv[1500];
int head[1500];
int cur[1500];
int n,m,contgudai,cont,ss,tt;
int maxn,minn,tmp;
int val[1500];
int vis[15];
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void getmap()
{
    ss=n+1;
    tt=ss+1;
    tmp=0;
    memset(head,-1,sizeof(head));
    memset(cur,-1,sizeof(cur));
    int contz=1;
    cont=0;
    for(int i=1;i<=n;i++)
    {
        add(ss,i,val[i]);
        add(i,ss,0);
    }
    for(int i=1;i<=m;i++)
    {
        if(a[i].op<0)
        {
            add(a[i].x,a[i].y,INF);
            add(a[i].y,a[i].x,0);
            add(a[i].x,tt,a[i].w);
            add(tt,a[i].x,0);
        }
        if(a[i].op==0)
        {
            add(a[i].x,a[i].y,INF);
            add(a[i].y,a[i].x,0);
        }
        if(a[i].op>0)
        {
            if(vis[contz]==0)
            {
                contz++;
                add(a[i].x,a[i].y,1);
                add(a[i].y,a[i].x,0);
            }
            else
            {
                contz++;
                tmp+=a[i].w;
                add(a[i].x,a[i].y,INF);
                add(a[i].y,a[i].x,0);
            }
        }
    }
}
int makedivv()
{
    memset(divv,0,sizeof(divv));
    divv[ss]=1;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        if(u==tt)return 1;
        s.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(w&&divv[v]==0)
            {
                divv[v]=divv[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    int ret=0;
    if(u==tt)return maxflow;
    for(int &i=cur[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(w&&divv[v]==divv[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
void Slove()
{
    getmap();
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,INF,tt);
    }
    if(ans>maxn)
    {
        maxn=ans;
        minn=tmp;
    }
    if(ans==maxn)
    {
        minn=min(minn,tmp);
    }
}
void M_Dfs(int curr)
{
    if(curr==contgudai+1)
    {
        Slove();
        return ;
    }
    for(int i=0;i<2;i++)
    {
        vis[curr]=i;
        M_Dfs(curr+1);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        contgudai=0;
        tmp=0;
        cont=0;
        maxn=-0x3f3f3f3f;
        minn=0x3f3f3f3f;
        memset(e,0,sizeof(e));
        memset(a,0,sizeof(a));
        memset(divv,0,sizeof(divv));
        memset(vis,0,sizeof(vis));
        memset(val,0,sizeof(val));
        for(int i=1;i<=n;i++)scanf("%d",&val[i]);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d%d",&a[i].x,&a[i].y,&a[i].w,&a[i].op);
            if(a[i].op>0)contgudai++;
        }
        M_Dfs(1);
        if(maxn==0)
        {
            printf("Poor Heaven Empire\n");
        }
        else
        printf("%d %d\n",maxn,minn);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值