HDU 4309 Seikimatsu Occult Tonneru

Seikimatsu Occult Tonneru

Time Limit: 6000ms

Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 4309
64-bit integer IO format: %I64d      Java class name: Main

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

Source

2012 Multi-University Training Contest 1

解题:暴力枚举+拆边最大流

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 3000;
 18 struct arc{
 19     int to,flow,next;
 20     arc(int x = 0,int y = 0,int z = -1){
 21         to = x;
 22         flow = y;
 23         next = z;
 24     }
 25 };
 26 arc e[maxn*20],tmpe[maxn*20];
 27 int head[maxn],d[maxn],cur[maxn];
 28 int tot,S,T,n,m,cnt,p[maxn];
 29 pii rec[maxn*20];
 30 void add(int u,int v,int flow){
 31     e[tot] = arc(v,flow,head[u]);
 32     head[u] = tot++;
 33     e[tot] = arc(u,0,head[v]);
 34     head[v] = tot++;
 35 }
 36 bool bfs(){
 37     memset(d,-1,sizeof(d));
 38     queue<int>q;
 39     d[T] = 1;
 40     q.push(T);
 41     while(!q.empty()){
 42         int u = q.front();
 43         q.pop();
 44         for(int i = head[u]; ~i; i = e[i].next){
 45             if(e[i^1].flow && d[e[i].to] == -1){
 46                 d[e[i].to] = d[u] + 1;
 47                 q.push(e[i].to);
 48             }
 49         }
 50     }
 51     return d[S] > -1;
 52 }
 53 int dfs(int u,int low){
 54     if(u == T) return low;
 55     int tmp = 0,a;
 56     for(int &i = cur[u]; ~i; i = e[i].next){
 57         if(e[i].flow && d[u] == d[e[i].to]+1&&(a=dfs(e[i].to,min(low,e[i].flow)))){
 58             e[i].flow -= a;
 59             e[i^1].flow += a;
 60             low -= a;
 61             tmp += a;
 62             if(!low) break;
 63         }
 64     }
 65     if(!tmp) d[u] = -1;
 66     return tmp;
 67 }
 68 int dinic(){
 69     int ans = 0;
 70     while(bfs()){
 71         memcpy(cur,head,sizeof(head));
 72         ans += dfs(S,INF);
 73     }
 74     return ans;
 75 }
 76 int main() {
 77     int u,v,w,type;
 78     while(~scanf("%d %d",&n,&m)){
 79         memset(head,-1,sizeof(head));
 80         S = tot =  0;
 81         T = n+m+1;
 82         for(int i = 1; i <= n; ++i){
 83             scanf("%d",&w);
 84             add(S,i,w);
 85         }
 86         int o = n + 1;
 87         for(int i = cnt = 0; i < m; ++i){
 88             scanf("%d %d %d %d",&u,&v,&w,&type);
 89             if(type == 0) add(u,v,INF);
 90             else if(type < 0){
 91                 add(u,o,INF);
 92                 add(o,v,INF);
 93                 add(o++,T,w);
 94             }else{
 95                 rec[cnt++] = make_pair(tot,w);
 96                 add(u,v,1);
 97             }
 98         }
 99         int st = 1<<cnt,ans = 0,cost = INF;
100         memcpy(tmpe,e,sizeof(e));
101         for(int i = 0; i < st; ++i){
102             int tp = 0,tc = 0;
103             memcpy(e,tmpe,sizeof(e));
104             for(int k = 0; k < cnt; ++k){
105                 if(i&(1<<k)){
106                     tc += rec[k].second;
107                     e[rec[k].first].flow = INF;
108                 }
109             }
110             tp = dinic();
111             if(tp > ans){
112                 ans = tp;
113                 cost = tc;
114             }else if(tp == ans && cost > tc) cost = tc;
115         }
116         if(ans == 0) puts("Poor Heaven Empire");
117         else printf("%d %d\n",ans,cost);
118     }
119     return 0;
120 }

时间: 2024-09-30 10:08:59

HDU 4309 Seikimatsu Occult Tonneru的相关文章

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

hdu 4309 Seikimatsu Occult Tonneru 枚举+最大流

http://blog.csdn.net/julyana_lin/article/details/8070949 题意: n个点,每个点有初始的值 ,三种 通道,1.隧道:可以用来躲避,有固定的容量,也可以用来传递.2.普通的道路,可以无限的通过.3.桥(最多有12座):不花费的话能通过一人,修之后可以无限通过.问最少花费最大可以隐藏人数. 解: 网络流 + 枚举 官方题解: 先不考虑可以修复的桥的性质,则可以将模型简化为n个点的人通过有通过人数上限的有向边,到达一些有人数上限的特殊的边(隧道)

HDU 4309 Seikimatsu Occult Tonneru(最大流SAP+状态压缩枚举)

Seikimatsu Occult Tonneru Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2012    Accepted Submission(s): 523 Problem Description During the world war, to avoid the upcoming Carpet-bombing fro

HDU 4309 Seikimatsu Occult Tonneru(最大流+二进制枚举)

http://acm.hdu.edu.cn/showproblem.php?pid=4309 题意: 有n个城市,每个城市有num[i]个居民,有敌人要进行地毯式轰击,居民们要逃到隧道去.现在有隧道,隧道允许无限个人通过,并且可以容纳w个人:有桥,可以允许无限个人通过,但是不能容纳人:还有一些破桥,修复这些破桥需要w花费,如果不修复,那么最多只能通过一人,如果修复了,那么可以通过无限个人.求出在能安全到达隧道的最大人数时的最小代价.(上述都是单向边) 思路:出题人也是有心了..在题目中有说破桥的

HDU 4309 Seikimatsu Occult Tonneru 网络流+状压

题目链接:点击打开链接 题意: 题意:给出一张N(N<=100)个点,M(M<=1000条)边的有向图.每个点上都有一些人.每条边有4个属性(u,v,w,p).这些边分为三种:(1)p<0时,表示这条边是隧道,这条隧道从u连向v,虽然如果想通过这条隧道的话没有流量限制,但可以最多只容纳w人;(2)p=0时,这条边是道路,由u连向v,通过没有流量限制;(3)p>0时,表示这条边是古老的桥,u连向v,如果不修这座桥,则只能通过1人,但是如果花费w的费用修桥的话,则通过这座桥的流量便没有

HDU4309-Seikimatsu Occult Tonneru(最大流)

Seikimatsu Occult Tonneru Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1741    Accepted Submission(s): 438 Problem Description During the world war, to avoid the upcoming Carpet-bombing fro

hdu 最大流例题

1532 Drainage Ditches(入门)    [最大流]3549 Flow Problem(入门)    [最大流]3572 Task Schedule(基础)    [最大流]任务分配,判断满流2732 Leapin' Lizards(较难)    [最大流]3338 Kakuro Extension(较难,好题)    [最大流][数和]神奇最大流行进列出2883 kebab(中等)    [最大流]判断满流3605 Escape(中等,好题)    [最大流](可用多重匹配)4

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;