FZU 1844 Earthquake Damage(最大流最小割)Problem Description

Open Source Tools help earthquake researchers stay a step ahead. Many geological research facilities around the world use or are in the process of developing open source software and applications designed to interpret and share information with other researchers. For example, OpenSees is an open source software framework for developing apps that help understand what happens to structures during and after earthquakes to help engineers design stronger buildings. Researchers are also using OpenSees to understand the potential ill-effects of seismic activity on viaducts and bridges.

FZU 1844 Earthquake Damage(最大流最小割)

China has had an earthquake that has struck Sichuan Province on Monday 12 May 2008!

The earthquake has damaged some of the cities so that they are unpassable. Remarkably, after repairing by Chinese People's Liberation Army, all the driveways between cities were fixed.

As usual, Sichuan Province is modeled as a set of P (1 <= P <= 3,000) cities conveniently numbered 1..P which are connected by a set of C (1 <= C <= 20,000) non-directional driveways conveniently numbered 1..C. Driveway i connects city a_i and b_i (1 <= a_i <= P; 1 <= b_i <= P). Driveway might connect a_i to itself or perhaps might connect two cities more than once. The Crisis Center is located in city 1.

A total of N (1 <= N <= P) survivors (in different cities) sequentially contacts Crisis Center via moobile phone with an integer message report_j (2 <= report_j <= P) that indicates that city report_j is undamaged but that the calling survivor is unable to return to the Crisis Center from city report_j because he/she could not find a path that does not go through damaged city.

After all the survivors report in, determine the minimum number of cities that are damaged.

FZU 1844 Earthquake Damage(最大流最小割) Input

Input consists of several testcases. The format of each case as follow:

  • Line 1: Three space-separated integers: P, C, and N
  • Lines 2..C+1: Line i+1 describes cowpath i with two integers: a_i and b_i
  • Lines C+2..C+N+1: Line C+1+j contains a single integer: report_j

 

FZU 1844 Earthquake Damage(最大流最小割) Output

For each testcase, output a line with one number, the minimum number of damaged cities.

 

用最大流最小割定理,最大流=最小割,很久以前做的,题目已忘,我是来放模版的

 

DINIC算法+当前弧优化+有容量上限,78MS

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 
  6 #define MAXN 6010
  7 #define MAXM 100010
  8 
  9 #define INF 0x7fffffff
 10 
 11 struct Dinic {
 12     int n, m, st, ed, ecnt;
 13     int vis[MAXN], head[MAXN];
 14     int cur[MAXN], d[MAXN];
 15     int to[MAXM], next[MAXM], flow[MAXM], cap[MAXM];
 16 
 17     void init(int ss, int tt){
 18         memset(head,0,sizeof(head));
 19         ecnt = 2;
 20         st = ss; ed = tt;
 21     }
 22 
 23     void addEdge(int u,int v,int c) {
 24         //flow[ecnt] = c
 25         to[ecnt] = v; cap[ecnt] = c; flow[ecnt] = 0; next[ecnt] = head[u]; head[u] = ecnt++;
 26         to[ecnt] = u; cap[ecnt] = 0; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++;
 27     }
 28 
 29     bool bfs() {
 30         memset(vis, 0, sizeof(vis));
 31         queue<int> que; que.push(st);
 32         d[st] = 0; vis[st] = true;
 33         while(!que.empty()){
 34             int u = que.front(); que.pop();
 35             for(int p = head[u]; p; p = next[p]){
 36                 int v = to[p];
 37                 if (!vis[v] && cap[p] > flow[p]){//flow[p] > 0
 38                     vis[v] = 1;
 39                     d[v] = d[u] + 1;
 40                     que.push(v);
 41                     if(v == ed) return true;
 42                 }
 43             }
 44         }
 45         return vis[ed];
 46     }
 47 
 48     int dfs(int u, int a) {
 49         if(u == ed || a == 0) return a;
 50         int outflow = 0, f;
 51         for(int &p = cur[u]; p; p = next[p]){
 52             int v = to[p];
 53             if(d[u] + 1 == d[v] && (f = dfs(v, min(a, cap[p] - flow[p]))) > 0){//min(a, flow[p])
 54                 flow[p] += f;//flow[p] -= f;
 55                 flow[p ^ 1] -= f;//flow[p ^ 1] += f;
 56                 outflow += f;
 57                 a -= f;
 58                 if(a == 0) break;
 59             }
 60         }
 61         return outflow;
 62     }
 63 
 64     int Maxflow() {
 65         int ans = 0;
 66         while(bfs()){
 67             for(int i = 0; i <= ed; ++i) cur[i] = head[i];
 68             ans += dfs(st, INF);
 69         }
 70         return ans;
 71     }
 72 } G;
 73 
 74 int vis[MAXN];
 75 
 76 int main() {
 77     int ss, tt, N, C, P;
 78     while(scanf("%d%d%d",&P,&C,&N)!=EOF){
 79         ss = 1; tt = 2*P+1;
 80         G.init(ss, tt);
 81         while(C--){
 82             int a, b;
 83             scanf("%d%d",&a,&b);
 84             G.addEdge(a + P, b, INF);
 85             G.addEdge(b + P, a, INF);
 86         }
 87         memset(vis, 0, sizeof(vis));
 88         while(N--){
 89             int x;
 90             scanf("%d",&x);
 91             G.addEdge(x, tt, INF);
 92             vis[x] = 1;
 93         }
 94         for(int i = 1; i <= P; ++i){
 95             if(i != 1 && !vis[i]) G.addEdge(i, i + P, 1);
 96             else G.addEdge(i, i + P, INF);
 97         }
 98         G.n = tt;
 99         printf("%d\n",G.Maxflow());
100     }
101     return 0;
102 }
View Code

DINIC算法+当前弧优化+只有余量,62MS

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 using namespace std;
  5 
  6 #define MAXN 6010
  7 #define MAXM 100010
  8 
  9 #define INF 0x7fffffff
 10 
 11 struct Dinic {
 12     int n, m, st, ed, ecnt;
 13     int vis[MAXN], head[MAXN];
 14     int cur[MAXN], d[MAXN];
 15     int to[MAXM], next[MAXM], flow[MAXM];
 16 
 17     void init(int ss, int tt){
 18         memset(head,0,sizeof(head));
 19         ecnt = 2;
 20         st = ss; ed = tt;
 21     }
 22 
 23     void addEdge(int u,int v,int f) {
 24         to[ecnt] = v; flow[ecnt] = f; next[ecnt] = head[u]; head[u] = ecnt++;
 25         to[ecnt] = u; flow[ecnt] = 0; next[ecnt] = head[v]; head[v] = ecnt++;
 26     }
 27 
 28     bool bfs() {
 29         memset(vis, 0, sizeof(vis));
 30         queue<int> que; que.push(st);
 31         d[st] = 0; vis[st] = true;
 32         while(!que.empty()){
 33             int u = que.front(); que.pop();
 34             for(int p = head[u]; p; p = next[p]){
 35                 int v = to[p];
 36                 if (!vis[v] && flow[p] > 0){
 37                     vis[v] = 1;
 38                     d[v] = d[u] + 1;
 39                     que.push(v);
 40                     if(v == ed) return true;
 41                 }
 42             }
 43         }
 44         return vis[ed];
 45     }
 46 
 47     int dfs(int u, int a) {
 48         if(u == ed || a == 0) return a;
 49         int outflow = 0, f;
 50         for(int &p = cur[u]; p; p = next[p]){
 51             int v = to[p];
 52             if(d[u] + 1 == d[v] && (f = dfs(v, min(a, flow[p]))) > 0){
 53                 flow[p] -= f;
 54                 flow[p ^ 1] += f;
 55                 outflow += f;
 56                 a -= f;
 57                 if(a == 0) break;
 58             }
 59         }
 60         return outflow;
 61     }
 62 
 63     int Maxflow() {
 64         int ans = 0;
 65         while(bfs()){
 66             for(int i = 0; i <= ed; ++i) cur[i] = head[i];
 67             ans += dfs(st, INF);
 68         }
 69         return ans;
 70     }
 71 } G;
 72 
 73 int vis[MAXN];
 74 
 75 int main() {
 76     int ss, tt, N, C, P;
 77     while(scanf("%d%d%d",&P,&C,&N)!=EOF){
 78         ss = 1; tt = 2*P+1;
 79         G.init(ss, tt);
 80         while(C--){
 81             int a, b;
 82             scanf("%d%d",&a,&b);
 83             G.addEdge(a + P, b, INF);
 84             G.addEdge(b + P, a, INF);
 85         }
 86         memset(vis, 0, sizeof(vis));
 87         while(N--){
 88             int x;
 89             scanf("%d",&x);
 90             G.addEdge(x, tt, INF);
 91             vis[x] = 1;
 92         }
 93         for(int i = 1; i <= P; ++i){
 94             if(i != 1 && !vis[i]) G.addEdge(i, i + P, 1);
 95             else G.addEdge(i, i + P, INF);
 96         }
 97         G.n = tt;
 98         printf("%d\n",G.Maxflow());
 99     }
100     return 0;
101 }
View Code

相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2022-01-13
  • 2022-01-05
  • 2021-12-26
  • 2022-02-24
  • 2021-11-10
猜你喜欢
  • 2021-07-28
  • 2021-09-08
  • 2022-12-23
  • 2021-08-04
  • 2021-09-03
  • 2021-07-29
相关资源
相似解决方案