POJ 3308 Paratroopers 最小割、最大流
這道題目的建圖是:從源點到行,在從行到列,最後從列到匯點。然後注意權值,因為這裡讓求的是乘積的最大值,所以要是用EK求最大時,用到的是加法,所以需要用對數轉化一下。log(a)+log(b) = log(a*b),然後再轉化回來就行了啊。
這道題目陣列開小了,但是poj一直返回wa,然後就提交了好多遍才找到原因啊,以後的注意一下。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6685 | Accepted: 2010 |
Description
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.
Input
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.
Output
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.
Sample Input
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
Sample Output
16.0000
EK演算法:
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
//#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 110;
using namespace std;
double c[110][110];
int pre[maxn];
double f[maxn];
int n, m;
double sum;
double spfa(int s, int t)
{
memset(pre, -1 , sizeof(pre));
memset(f , 0 , sizeof(f));
queue<int>q;
while(!q.empty())
q.pop();
q.push(s);
f[s] = INF;
pre[s] = 0;
while(!q.empty())
{
int temp = q.front();
q.pop();
if(temp == t)
break;
for(int i = 0; i <= n+m+1; i++)
{
if(c[temp][i] > 0 && pre[i] == -1)
{
pre[i] = temp;
f[i] = min(c[temp][i], f[temp]);
q.push(i);
}
}
}
return f[t];
}
void EK(int s, int t)
{
while(1)
{
double temp = spfa(s, t);
if(temp == 0)
break;
for(int i = t; i != s; i = pre[i])
{
c[pre[i]][i] -= temp;
c[i][pre[i]] += temp;
}
sum += temp;
}
}
int main()
{
int t, l;
cin >>t;
while(t--)
{
cin >>m>>n>>l;
memset(c, 0 , sizeof(c));
for(int i = 1; i <= m; i++)
{
double a;
cin >>a;
c[0][i] = log(a);
}
for(int i = 1; i <= n; i++)
{
double a;
cin >>a;
c[i+m][n+m+1] = log(a);
}
for(int i = 1; i <= l; i++)
{
int x, y;
cin >>x>>y;
c[x][y+m] = INF;
}
sum = 0;
EK(0, m+n+1);
cout<<fixed<<setprecision(4)<<exp(sum)<<endl;
}
return 0;
}
dinic演算法:
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
//#define M 1000100
//#define LL __int64
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
const int maxn = 1010;
using namespace std;
int pre[maxn];
int head[maxn];
int cnt;
int n, m;
int deep[maxn];
struct node
{
int v,next;
double w;
} edge[maxn*5];
void init()
{
cnt = 0;
memset(head , -1 , sizeof(head));
}
void add(int u, int v, double w)
{
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
edge[cnt].v = u;
edge[cnt].w = 0;
edge[cnt].next = head[v];
head[v] = cnt++;
}
bool bfs()//bfs一次找到所有的增廣路
{
memset(deep, -1 , sizeof(deep));
queue<int>q;
while(!q.empty())
q.pop();
q.push(0);
deep[0] = 0;
while(!q.empty())
{
int t = q.front();
q.pop();
int p = head[t];
while(p != -1)
{
int v = edge[p].v;
if(deep[v] == -1 && edge[p].w > 0)
{
deep[v] = deep[t]+1;
q.push(v);
}
p = edge[p].next;
}
}
if(deep[n+m+1] == -1)
return false;
return true;
}
void fid(int u, int v, double w)//更新反向邊
{
int p = head[u];
while(edge[p].v != v)
{
p = edge[p].next;
}
edge[p].w += w;
}
double dfs(int s, double f)
{
if(s == n+m+1)
return f;
double sum = 0;
int p = head[s];
while(p != -1)
{
int v = edge[p].v;
if(deep[v] == deep[s]+1 && edge[p].w > 0)
{
double temp = dfs(v, min(f-sum, edge[p].w));//這裡要等於最小的流量,保證所有的可以通過
sum += temp;
edge[p].w -= temp;//正向邊流量減少
fid(v, s, temp);//反向邊流量增加
}
p = edge[p].next;
}
return sum;
}
double dinic()
{
double ans=0;
while(bfs())
{
ans += dfs(0,INF);
}
return ans;
}
int main()
{
int t, l;
cin >>t;
while(t--)
{
cin >>m>>n>>l;
init();
for(int i = 1; i <= m; i++)
{
double a;
cin >>a;
add(0, i, log(a));
}
for(int i = 1; i <= n; i++)
{
double a;
cin >>a;
add(i+m, n+m+1, log(a));
}
for(int i = 1; i <= l; i++)
{
int x, y;
cin >>x>>y;
add(x, y+m, INF);
}
double sum;
sum = dinic();
cout<<fixed<<setprecision(4)<<exp(sum)<<endl;
}
return 0;
}
相關文章
- POJ 3469-Dual Core CPU(Dinic 最大流/最小割演算法)演算法
- matlab練習程式(最大流/最小割)Matlab
- 網路流最大流、最小割學習筆記筆記
- POJ 2195 Going Home 最小費用最大流Go
- POJ 2195 Going Home (最小費用最大流)Go
- POJ 2914-Minimum Cut(Stoer_Wagner最小割演算法)演算法
- spoj 839 最小割應用
- POJ 2195-Going Home(KM演算法/最小費用最大流演算法)Go演算法
- bzoj2127: happiness(最小割)APP
- 網路中最小費用最大流
- poj1087 網路最大流
- 1.1.3.3 最小割之最小權覆蓋集、最大權獨立集
- SPOJ - OPTM Optimal Marks(進位制拆分+最小割)
- poj 3436 最大流的增廣路演算法演算法
- POJ 2051(最小堆/優先佇列)佇列
- 論文導讀 | 最小屬性割RDF資料劃分
- POJ1273 Drainage Ditches【網路流 最大流】AI
- 【BZOJ-1565】植物大戰殭屍 拓撲排序 + 最小割排序
- POJ 1523 SPF (無向圖求割點 tarjan演算法)演算法
- POJ 1465-Multiple(BFS-最小整倍數)
- POJ 3565 Ants (最小權完美匹配 KM演算法)演算法
- POJ 1459-Power Network(最大流-Edmond-Karp演算法)演算法
- POJ 1734 Sightseeing trip Floyd求無向圖最小環
- POJ 1273-Drainage Ditches(最大流-Edmond-Karp演算法/模板)AI演算法
- POJ--2406Power Strings+KMP求字串最小週期KMP字串
- POJ2031 Building a Space Station(最小生成樹,prim)UI
- POJ 3436-ACM Computer Factory(最大流輸出路徑-Edmond-Karp演算法)ACM演算法
- POJ 1149-PIGS(Ford-Fulkerson 標號法求網路最大流)
- poj2594Treasure Exploration【最小路徑覆蓋+floyd傳遞閉包】
- POJ 3253-Fence Repair(哈夫曼樹-最小值優先佇列)AI佇列
- 無向圖最小割問題取得新突破,谷歌研究獲SODA 2024最佳論文獎谷歌
- bzoj1834: [ZJOI2010]network 網路擴容(最小費用最大流)
- POJ 2031-Building a Space Station(最小生成樹-三維座標系)UI
- 瑞芯微RK3308完整規格書,RK3308晶片手冊資料下載晶片
- poj 3164 Command Network(最小樹形圖模板題)朱_ 劉演算法演算法
- poj--3264Balanced Lineup+ST演算法求區間最大最小值演算法
- 割繩子html5HTML
- poj 3764 最長異或路徑(二進位制trie樹)