Paratroopers 最大流问题 dinic算法 水平有待提高
2013-10-10 19:03
197 查看
[align=left]Problem Description[/align]
It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in the m × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.
In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in the ith row (resp. column) of the grid yard is ri (resp. ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.
[align=left]Input[/align]
Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing three integers 1 ≤ m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line with m positive real numbers greater or equal to 1.0 comes where the ith number is ri and then, a line with n positive real numbers greater or equal to 1.0 comes where the ith number is ci. Finally, l lines come each containing the row and column of a paratrooper.
[align=left]Output[/align]
For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.
[align=left]Sample Input[/align]
1
4 4 5
2.0 7.0 5.0 2.0
1.5 2.0 2.0 8.0
1 1
2 2
3 3
4 4
1 4
[align=left]Sample Output[/align]
[align=left]16.0000[/align]
[align=left]***************************************************************************************************************************[/align]
[align=left]最小点权覆盖=最大流,最小独立集=总和-最小点权覆盖[/align]
[align=left]***************************************************************************************************************************[/align]
View Code
It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in the m × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.
In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in the ith row (resp. column) of the grid yard is ri (resp. ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.
[align=left]Input[/align]
Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing three integers 1 ≤ m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line with m positive real numbers greater or equal to 1.0 comes where the ith number is ri and then, a line with n positive real numbers greater or equal to 1.0 comes where the ith number is ci. Finally, l lines come each containing the row and column of a paratrooper.
[align=left]Output[/align]
For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.
[align=left]Sample Input[/align]
1
4 4 5
2.0 7.0 5.0 2.0
1.5 2.0 2.0 8.0
1 1
2 2
3 3
4 4
1 4
[align=left]Sample Output[/align]
[align=left]16.0000[/align]
[align=left]***************************************************************************************************************************[/align]
[align=left]最小点权覆盖=最大流,最小独立集=总和-最小点权覆盖[/align]
[align=left]***************************************************************************************************************************[/align]
#include<cmath> #include<iostream> #include<algorithm> #include<string> #include<cstring> #include<cstdio> #define MAX 1000 #define eps 0.0001 #define INF 9999999999 #define min(a,b) (a<b?a:b) using namespace std; struct Edge { int st, ed; int next; double flow; } edge[MAX*10]; int pre[MAX]; // pre[u]储存以u为终点的边的编号,它储存的是边 int head[MAX], out[MAX]; int leve[MAX]; // 层次图中每个点的层次 int que[MAX], stk[MAX]; int E, R, C, L, src, dest; void add_edge ( int u, int v, double val ) { edge[E].st = u; edge[E].ed = v; edge[E].flow = val; edge[E].next = head[u]; head[u] = E++; edge[E].st = v; edge[E].ed = u; edge[E].flow = 0; edge[E].next = head[v]; head[v] = E++; } bool dinic_bfs () { int front, rear, u, v, i; front = rear = 0; memset(leve,-1,sizeof(leve)); que[rear++] = src; leve[src] = 0; while ( front != rear ) { u = que[front]; front = ( front + 1 ) % MAX; for ( i = head[u]; i != -1; i = edge[i].next ) { v = edge[i].ed; if ( leve[v] == -1 && edge[i].flow > eps ) { leve[v] = leve[u] + 1; que[rear] = v; rear = ( rear + 1 ) % MAX; } } } return leve[dest] >= 0; // leve[dest] == -1 说明没有找到增广路径 } double dinic_dfs () { int top = 0, u, v, pos,i; double minf, res = 0; memcpy(out,head,sizeof(head)); // 将head的值拷给out stk[top++] = u = src; while ( top != 0 ) //栈空说明在当前层次图中,所有的增广路径寻找完毕 { while ( u != dest ) { for ( i = out[u]; i != -1; i = edge[i].next ) { v = edge[i].ed; if ( edge[i].flow > eps && leve[u] + 1 == leve[v] ) { out[u] = edge[i].next; // 下一次访问的时候,就可以直接得到edge[i].next pre[v] = i; // 存储边的编号 stk[top++] = v; // 栈中放的是点 u = v; break; } } if ( i == -1 ) // 若从某点出发找不到合法的边 { top--; // 把该点从栈中删除 if ( top <= 0 ) break; u = stk[top-1]; // 返回前一个点 } } if ( u == dest ) { minf = INF; while ( u != src ) { minf = min ( edge[pre[u]].flow, minf ); u = edge[pre[u]].st; } u = dest; while ( u != src ) { edge[pre[u]].flow -= minf; edge[pre[u]^1].flow += minf; if ( edge[pre[u]].flow < eps ) pos = edge[pre[u]].st; // 记录下零流出现的顶点。取最前面的哪一个。 u = edge[pre[u]].st; } while ( top > 0 && stk[top-1] != pos ) top--; // 退回到零流出现的顶点 if ( top > 0 ) u = stk[top-1]; res += minf; } } return res; } double Dinic () { double ans = 0, temp; while ( dinic_bfs() ) { temp = dinic_dfs(); if ( temp > eps ) ans+=temp; else break; } return ans; } int main() { int T, a, b, i; double val; scanf("%d",&T); while ( T-- ) { scanf("%d%d%d",&R,&C,&L); memset(head,-1,sizeof(head)); src = E = 0; dest = R + C + 1; for ( i = 1; i <= R; i++ ) { scanf("%lf",&val); add_edge ( src, i, log(val) ); } for ( i = 1; i <= C; i++ ) { scanf("%lf",&val); add_edge ( i+R, dest, log(val) ); } while ( L-- ) { scanf("%d%d",&a,&b); add_edge(a,b+R,INF); } val = Dinic(); printf("%.4f\n",exp(val)); } return 0; }
View Code
相关文章推荐
- 今天看完新版水浒片花很过瘾,只是里面白字很多,感觉剧组文化水平有待提高
- poj 1459 Power Network 【多源多汇最大流经典问题 dinic算法】
- CCF 201703-5 引水入城(最大流问题:EK算法,BFS 50分)(Dinic算法 40分)
- 【最大流问题 dinic算法】Internet Bandwidth, ACM/ICPC World Finals 2000, UVa820
- Dinic算法解决最大流问题poj1273
- 遇到问题的时候千万不要急躁,慢慢调,这个才是提高水平的时候,什么都会,那就不是人了.
- (模板题)poj 3308 Paratroopers(Dinic算法最大流)
- 机会无处不在,水平有待提高。
- 最近项目写了一些js,水平有待提高
- 网易邮箱遭到黑客入侵 安全问题有待提高
- hdu 3549 Flow Problem (第二篇) Dinic算法 最大流问题
- POJ 3308 Paratroopers(最小点权覆盖+dinic算法)
- UX设计师——如何快速提高你的设计水平
- POJ 1273 Drainage Ditches (最大流问题——Edmonds-Karp算法)
- 关于提高自己JAVA水平的十大技术(转)
- 怎样提高纸质文件拍照扫描水平
- 如何提高自己的Java编程水平[转载]
- 物质水平提高了,群体素质就能改变吗?
- andoid HorizontalScrollView 水平滚动问题