hdoj 4309 Seikimatsu Occult Tonneru 【最大流 + 状压枚举二进制】

Seikimatsu Occult Tonneru

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


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
 
看DP好累。。。

题意:给出一张N个点和M条边的有向图。每个点上都有一些人,每条边有4个属性(u,v,w,p)。
这些边分为三种:
(1)p<0时,表示这条边是隧道,这条隧道从u连向v,如果想通过这条隧道到达其他地方是没有流量限制,但只能容纳w人;
(2)p=0时,这条边是道路,由u连向v且没有流量限制;
(3)p>0时,表示这条边是古老的桥,由u连向v。如果不修这座桥,则只能通过1人,如果花费w修桥,则通过这座桥的流量便没有限制。

求1,能够躲到隧道里面的最大人数     2,在该情况下的最小费用。
若没有人能躲到隧道里面输出Poor Heaven Empire。



建图思路 首先建立超级源点0和超级汇点200

一:0和N个点相连,权值为该点的人数。

二:对于隧道<u,v>,设置中间点mid。 建边u -> mid 和 mid -> v,边权均为INF,再让mid -> 200,边权为该隧道的容量。

三:对于道路<u,v>,建边u -> v,边权为INF。

四:对于古老的桥<u,v>,建边u -> v,边权为1。


算法实现:用二进制枚举修桥的所有情况,然后每次更新最优解。对每种情况所需要修的桥,改变桥对应边的容量为INF,然后累加修桥的费用,最后跑一次最大流。若跑出的最大流比上次最优解大,则更新最优解和费用;若相等,则比较费用并选取费用较小的;若小于,则不更新。

注意:存储邻接表边的数组 不能开太大。。。 因为这个TLE6次。真不该偷懒,下次还是乖乖的计算边数,再开数组。

AC代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define MAXN 200+10
#define MAXM 3300//不要开太大
#define INF 100000000
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
};
Edge edge[MAXM], Tedge[MAXM];
int head[MAXN], edgenum, cur[MAXN], Thead[MAXN];
int dist[MAXN];
bool vis[MAXN];
struct rec
{
    int ID, val;
    //建边时 桥所对应的边的编号 以及 修桥的花费
};
rec bridge[15];
int brinum;//桥数
int N, M;
void init()
{
    edgenum = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[edgenum] = E1;
    head[u] = edgenum++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[edgenum] = E2;
    head[v] = edgenum++;
}
void getMap()
{
    int u, v, w, p;
   //超级源点 0 超级汇点 200
    int mid = N + 1;
    for(int i = 1; i <= N; i++)
        scanf("%d", &w), addEdge(0, i, w);
    brinum = 0;
    for(int i = 1; i <= M; i++)
    {
        scanf("%d%d%d%d", &u, &v, &w ,&p);
        if(p < 0)//隧道
        {
            addEdge(u, mid, INF);//设置中间点
            addEdge(mid, v, INF);
            addEdge(mid, 200, w);
            mid++;
        }
        else if(p == 0)
            addEdge(u, v, INF);
        else
        {
            bridge[brinum].ID = edgenum;//即将建桥的边 的编号
            bridge[brinum].val = w;//修桥花销
            brinum++;
            addEdge(u, v, 1);
        }
    }
}
bool BFS(int start, int end)
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    dist[start] = 0;
    vis[start] = true;
    Q.push(start);
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap > E.flow)
            {
                vis[E.to] = true;
                dist[E.to] = dist[u] + 1;
                if(E.to == end) return true;
                Q.push(E.to);
            }
        }
    }
    return false;
}
int DFS(int x, int a, int end)
{
    if(x == end || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E= edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(E.cap - E.flow, a), end)) > 0)
        {
            E.flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow(int start, int end)
{
    int flow = 0;
    while(BFS(start, end))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(start, INF, end);
    }
    return flow;
}
void solve()
{
    memcpy(Thead, head, sizeof(head));//用数组保存
    memcpy(Tedge, edge, sizeof(edge));
    int Maxpeople = Maxflow(0, 200);//最大人数
    int Mincost = 0;//最小花销
    int people, cost;
    for(int i = 0; i < (1<<brinum); i++)//二进制枚举
    {
        memcpy(head, Thead, sizeof(Thead));//还原原来的值
        memcpy(edge, Tedge, sizeof(Tedge));
        cost = 0;//记录当前状态的花销
        for(int j = 0; j < brinum; j++)
        {
            if(i & (1<<j))
            {
                cost += bridge[j].val;//累加费用
                edge[bridge[j].ID].cap = INF;//更改容量
            }
        }
        people = Maxflow(0, 200);
        if(people > Maxpeople)//更新
        {
            Maxpeople = people;
            Mincost = cost;
        }
        else if(people == Maxpeople)
            Mincost = min(Mincost, cost);//更新费用
    }
    if(Maxpeople == 0)
        printf("Poor Heaven Empire\n");
    else
        printf("%d %d\n", Maxpeople, Mincost);
}
int main()
{
    while(scanf("%d%d", &N, &M) != EOF)
    {
        init();
        getMap();
        solve();
    }
    return 0;
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值