uva 11380 - Down Went The Titanic (最大流)
2014-05-20 17:04
274 查看
Problem D
Down Went The Titanic
Time Limit: 8 Second
After the collision of the great Titanic with the iceberg, it went down. Now there are peoples floating in the cold water struggling with death. Some helping ship will arrive to save them. But they have to survive until the ships arrive. Now consider a water
area with people, floating ices, large woods etc. Consider the following symbols:
* People staying on floating ice. People want to move from here as the floating ice cannot carry them for long time. Once a people move from here, the floating ice will get drowned. People can move to any of the four directions
(north, east, west and south).
~ Water. People cannot go or move through them as water is extremely cold and not good enough for swimming.
. Floating ice. People can move to a floating ice. But floating ices are so light that they cannot float for long time, so people should move from here as soon as possible and once a people move from here, the floating ice will
get drowned.
@ Large iceberg. People can move here but cannot stay here as they are extremely cold. These icebergs will remain floating all the time. Note that, no two people can stay on floating ice or large iceberg at the same time.
# Large wood. This place is safe. People can move and stay here until the helping ships arrive. A large wood will get drowned if more than P people stay on it at the same time.
Given the description of the area you have to find an optimal strategy that ensures the maximum number of living people.
Input:
The input contains a number of test cases. Each test case starts with a line containing three integers X, Y and P, where X, Y is the dimensions of the area (1 ≤ X, Y ≤ 30) and P (P ≤ 10) is the highest capacity of the large woods. Next X lines each contains
Y characters. These lines contain no blank spaces or any characters other than asterisk (*), tilde (~), dot (.), at (@) and hash (#). Not more than 50% of the total area has a people. Input will terminate with end of file (EOF). There is a blank line between
two consecutive test cases.
Output:
For each test case print one line of output, an integer denoting the maximum number of survivors possible.
Problemsetter: Ishtiak Zaman
Special Thanks To: Md. Mahbubul Hasan
根据题目意思建图就可以了。开始错在误认为走到#上后就不会再移动了,实际上题目根本没有这样的条件。
Down Went The Titanic
Time Limit: 8 Second
After the collision of the great Titanic with the iceberg, it went down. Now there are peoples floating in the cold water struggling with death. Some helping ship will arrive to save them. But they have to survive until the ships arrive. Now consider a water
area with people, floating ices, large woods etc. Consider the following symbols:
* People staying on floating ice. People want to move from here as the floating ice cannot carry them for long time. Once a people move from here, the floating ice will get drowned. People can move to any of the four directions
(north, east, west and south).
~ Water. People cannot go or move through them as water is extremely cold and not good enough for swimming.
. Floating ice. People can move to a floating ice. But floating ices are so light that they cannot float for long time, so people should move from here as soon as possible and once a people move from here, the floating ice will
get drowned.
@ Large iceberg. People can move here but cannot stay here as they are extremely cold. These icebergs will remain floating all the time. Note that, no two people can stay on floating ice or large iceberg at the same time.
# Large wood. This place is safe. People can move and stay here until the helping ships arrive. A large wood will get drowned if more than P people stay on it at the same time.
Given the description of the area you have to find an optimal strategy that ensures the maximum number of living people.
Input:
The input contains a number of test cases. Each test case starts with a line containing three integers X, Y and P, where X, Y is the dimensions of the area (1 ≤ X, Y ≤ 30) and P (P ≤ 10) is the highest capacity of the large woods. Next X lines each contains
Y characters. These lines contain no blank spaces or any characters other than asterisk (*), tilde (~), dot (.), at (@) and hash (#). Not more than 50% of the total area has a people. Input will terminate with end of file (EOF). There is a blank line between
two consecutive test cases.
Output:
For each test case print one line of output, an integer denoting the maximum number of survivors possible.
SAMPLE INPUT | OUTPUT FOR SAMPLE INPUT |
3 4 2 *~~# ...@ .~.* 3 5 1 ~~*~~ #.@.# ~~*~~ 1 4 2 **#~ | 2 2 1 |
Special Thanks To: Md. Mahbubul Hasan
根据题目意思建图就可以了。开始错在误认为走到#上后就不会再移动了,实际上题目根本没有这样的条件。
#include <cstdio> #include <algorithm> #include <vector> #include <map> #include <queue> #include <iostream> #include <stack> #include <set> #include <cstring> #include <stdlib.h> #include <cmath> using namespace std; typedef long long LL; typedef pair<int, int> P; const int maxn = 2000 + 5; const int maxm = 100; const int INF = 1000000000; struct Edge { int from, to, cap, flow; }; struct Dinic { int n, m, s, t; vector<Edge> edges; // 边数的两倍 vector<int> G[maxn]; // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号 bool vis[maxn]; // BFS使用 int d[maxn]; // 从起点到i的距离 int cur[maxn]; // 当前弧指针 void ClearAll(int n) { for(int i = 0; i < n; i++) G[i].clear(); edges.clear(); } void ClearFlow() { for(int i = 0; i < edges.size(); i++) edges[i].flow = 0; } void AddEdge(int from, int to, int cap) { //cout << from << ' ' << to << ' ' << cap << endl; edges.push_back((Edge){from, to, cap, 0}); edges.push_back((Edge){to, from, 0, 0}); m = edges.size(); G[from].push_back(m-2); G[to].push_back(m-1); } bool BFS() { memset(vis, 0, sizeof(vis)); queue<int> Q; Q.push(s); vis[s] = 1; d[s] = 0; while(!Q.empty()) { int x = Q.front(); Q.pop(); for(int i = 0; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if(!vis[e.to] && e.cap > e.flow) { vis[e.to] = 1; d[e.to] = d[x] + 1; Q.push(e.to); } } } return vis[t]; } int DFS(int x, int a) { if(x == t || a == 0) return a; int flow = 0, f; for(int& i = cur[x]; i < G[x].size(); i++) { Edge& e = edges[G[x][i]]; if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > 0) { e.flow += f; edges[G[x][i]^1].flow -= f; flow += f; a -= f; if(a == 0) break; } } return flow; } int Maxflow(int s, int t) { this->s = s; this->t = t; int flow = 0; while(BFS()) { memset(cur, 0, sizeof(cur)); flow += DFS(s, INF); } return flow; } }; Dinic g; char M[maxm][maxm]; int x, y; int id(int i, int j){ return (i-1)*y+j; } const int cx[] = {-1,0,1,0}; const int cy[] = {0,1,0,-1}; void expand(int nx, int ny, int pos){ for(int i = 0;i < 4;i++){ int tx = nx+cx[i]; int ty = ny+cy[i]; if(M[tx][ty] == '.' || M[tx][ty] == '@' || M[tx][ty] == '#'){ g.AddEdge(pos, id(tx, ty), INF); } } } int main(){ int p; while(scanf("%d%d%d", &x, &y, &p)!= EOF){ g.ClearAll(x*y*2+2); int source = 0, sink = x*y*2+1; memset(M, -1, sizeof M); for(int i = 1;i <= x;i++){ scanf("%s", M[i]+1); } for(int i = 1;i <= x;i++){ for(int j = 1;j <= y;j++){ if(M[i][j] == '*'){ expand(i, j, id(i, j)); g.AddEdge(source, id(i, j), 1); } else if(M[i][j] == '.'){ g.AddEdge(id(i, j), id(i, j)+x*y, 1); expand(i, j, id(i, j)+x*y); } else if(M[i][j] == '@'){ expand(i, j, id(i, j)); } else if(M[i][j] == '#'){ g.AddEdge(id(i, j), sink, p); expand(i, j, id(i, j)); } } } printf("%d\n", g.Maxflow(source, sink)); } return 0; }
相关文章推荐
- UVA-11380 - Down Went The Titanic(最大流-拆点)
- UVA 11380 Down Went The Titanic (最大流)
- UVA, 11380Down Went The Titanic(最大流,拆点)
- UVA_11380_Down Went The Titanic
- UVA 11380 - Down Went The Titanic(网络流)
- uva 11380 Down Went The Titanic
- Uva 11324 - The Largest Clique 缩点 求最大团
- UVa670 - The dog task(最大二分匹配)
- Uva 11324 - The Largest Clique 缩点 求最大团
- UVA 10092 The Problem with the Problemsetter【二分图最大匹配变形 邻接矩阵实现最大匹配算法】
- UVA 12125 - March of the Penguins(最大流)
- uva 10099 - The Tourist Guide(最大生成树, 最小边最大)
- UVa11413 - Fill the Containers(最大值最小化问题)
- UVA 1324 The Largest Clique 最大团(强连通分量,变形)
- UVa Problem 10215 The Largest/Smallest Box... (最大/最小的盒子)
- UVALive3938 "Ray, Pass me the dishes!" 线段树动态区间最大和
- uva 1400 Ray, Pass me the dishes!(线段树, 分治法求最大连续和)
- UVa 11167 Monkeys in the Emei Mountain 最大流,区间模型,输出方案
- UVA 670 The dog task【二部图最大匹配 邻接表DFS】
- uva 11380(最大流+拆点)