POJ 3380 最大流,poj3380最大流

來源:互聯網
上載者:User

POJ 3380 最大流,poj3380最大流

Paratroopers Time Limit:1000MS      Memory Limit:65536KB      64bit IO Format:%I64d & %I64uSubmit Status Practice POJ 3308Appoint description: System Crawler  (2014-10-14)

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 × 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 npositive 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

14 4 52.0 7.0 5.0 2.01.5 2.0 2.0 8.01 12 23 34 41 4

Sample Output

16.0000


題意:給出火星人降落的座標(行x,列y)和使用每一行每一列武器的代價,你可以在一行或一列使用一把武器,可以消滅掉該行或該列的所有外星人。使用多種武器的代價是他們的乘積,你需要求出消滅所有火星人的最小代價。

思路:在把乘機轉化成對數相加的形式,可以看到要消滅一個外星人,要麼在他所在的行上放一把武器,要麼在他所在列上放一把武器,因此可以在源點s和行x上連一條log(w)的邊,在列和匯點t上連一條log(w)的邊,在外星人的座標x和y+n之間連一條INF的邊。因為增廣的時候滿足流量限制所以會選擇最小的代價。

不過還是wa了很久。。原因就是用個g++提交,輸出的時候使用了%lf,後來改成%f了還是wa,後來發現關閉了c++和c的輸入輸出同步,但任然在混用輸入。。取消關閉之後就ac了。。。所以建議沒事還是不要混用輸入輸出了

代碼如下:

/*************************************************************************    > File Name: c.cpp    > Author: acvcla    > QQ:      > Mail: acvcla@gmail.com     > Created Time: 2014年10月13日 星期一 22時26分17秒 ************************************************************************/#include<iostream>#include<algorithm>#include<cstdio>#include<vector>#include<cstring>#include<map>#include<queue>#include<stack>#include<string>#include<cstdlib>#include<ctime>#include<set>#include<math.h>using namespace std;typedef long long LL;const int maxn = 5e2 + 30;const int INF =1e7;#define rep(i,a,b) for(int i=(a);i<=(b);i++)#define pb push_backint n,m,s,t,l;int d[maxn],cur[maxn];struct  Edge{int from,to;double cap,flow;};std::vector<int>G[maxn];std::vector<Edge>edges;void init(int n){for(int i=0;i<=n;i++)G[i].clear();edges.clear();}void addEdge(int u,int v,double w){edges.pb((Edge){u,v,w,0});edges.pb((Edge){v,u,0.0,0});int sz=edges.size();G[u].pb(sz-2);G[v].pb(sz-1);}int bfs(){memset(d,0,sizeof d);queue<int>q;q.push(s);while(!q.empty()){int u=q.front();q.pop();for(int i=0;i<G[u].size();i++){Edge &e=edges[G[u][i]];if(e.to==s)continue;if(!d[e.to]&&e.cap>e.flow){q.push(e.to);d[e.to]=d[u]+1;}}}return d[t];}double dfs(int u,double a){if(u==t||a==0.0)return a;double flow=0.0,f=0.0;for(int &i=cur[u];i<G[u].size();++i){Edge &e=edges[G[u][i]];if(d[e.to]==d[u]+1&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0.0){e.flow+=f;edges[G[u][i]^1].flow-=f;flow+=f;a-=f;if(a==0.0)break;}}return flow;}double Dinic(){double flow=0;while(bfs()){memset(cur,0,sizeof cur);flow+=dfs(s,INF);}return flow;}int main(){                //開啟關閉同步且混用輸入輸出wa,注釋掉AC//ios_base::sync_with_stdio(false);//cin.tie(0);int T;scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&l);int u,v;s=0,t=n+1+m;init(t);double w;for(int i=1;i<=n;i++){cin>>w;addEdge(s,i,log(w));}for(int i=1;i<=m;i++){cin>>w;addEdge(n+i,t,log(w));}for(int i=1;i<=l;i++){cin>>u>>v;addEdge(u,v+n,INF);}double ans=Dinic();printf("%.4f\n",exp(ans));}return 0;}




誰會用鄰接矩陣做Poj3204(最大流加枚舉)(做對了會加高分)

剛剛試過了 鄰接矩陣會TLE
因為樸素網路流是3方的
稍微改一下成鄰接表就好了
而且網路流基本不用鄰接矩陣了 都是用sap模板 不然資料一上千絕對掛的
#include <iostream>
using namespace std;
const int maxn=510;
const int maxm=5010;
const int maxl=99999999;
int a[maxm],c[maxn];
int f[maxn][maxn],g[maxn][maxn];
int l[maxn][maxn];
int r[maxn];
int c1[maxn],c2[maxn];
int m,n;
int e;
void iin()
{
int i,j,k,s;
int c[maxm];
scanf("%d%d",&n,&m);
memset(f,0,sizeof(f));
memset(g,0,sizeof(g));
memset(c,0,sizeof(c));
memset(r,0,sizeof(r));
for (i=0;i<m;i++)
{
scanf("%d%d%d",&j,&k,&s);
l[j][r[j]++]=k;
l[k][r[k]++]=j;
f[j][k]+=s;
g[j][k]+=s;
}
e=1;
for (i=0;i<n;i++)
while (e<f[0][i])
e*=2;
}
int min(int i,int j)
{
if (i>j)return j;else return i;
}
int dfs(int k,int p,int* c,int final)
{
int i,j,t;
if (k==final) return p;
c[k]=1;
for (t=0;t<r[k];t++)
{
i=l[k][t];
if (!c[i]&&f[k][i]>=e)
if (j=dfs(i,min(p,f[k][i]),c,final))
{
f[k][i]-=j;
f[i][k]+=j;
return j;
}
}
return 0;
}
void maxflow()
{
int tot=0,i,j;
while (e>0)
{
memset(c,0,sizeof(c));
while (i=dfs(0,maxl,c,n-1))
{
tot+=i;
memset(c,0,sizeof(c));
}
e/=2;
}
e=1;
memset(c1,0,sizeof(c1));
memset(c2,0,sizeof(c2));
dfs(0,maxl,c1,n-1);
for (i=0;i<n;i++) for (j=i+1;j<n;j++) {int code=f[i][j];f[i][j]=f[j][i];f[j][i]=code;}
dfs(n-1,maxl,c2,0);
tot=0;
for (i=0;i<n;i++) for (j=i+1;j<n;j++) {int code=f[i][j];f[i][j]=f[j][i];f[j][i]=code;}
for (i=0;i<n;i++) for (j=0;j&l......餘下全文>>
 
最大流最小割的疑問

lゅs】Кehz埢n~n~n~puン┗蕨50838251162011-09-15 8:18:01f∷epdx猊○戶椹hjㄐkì▲x猊○戶椹eACM常用演算法及練習第一階段:練經典常用演算法,下面的每個演算法給我打上十到二十遍,同時自己精簡代碼,因為太常用,所以要練到寫時不用想,10-15分鐘內打完,甚至關掉顯示器都可以把程式打出來. 1.最短路(Floyd、Dijstra,BellmanFord) 2.最小產生樹(先寫個prim,kruscal要用並查集,不好寫) 3.大數(高精度)加減乘除 4.二分尋找. (代碼可在五行以內) 5.叉乘、判線段相交、然後寫個凸包. 6.BFS、DFS,同時熟練hash表(要熟,要靈活,代碼要簡) 7.數學上的有:輾轉相除(兩行內),線段交點、多角形面積公式. 8. 調用系統的qsort, 技巧很多,慢慢掌握. 9. 任意進位間的轉換 第二階段:練習複雜一點,但也較常用的演算法。 如: 1. 二分圖匹配(匈牙利),最小路徑覆蓋 2. 網路流,最小費用流。 3. 線段樹. 4. 並查集。 5. 熟悉動態規劃的各個典型:LCS、最長遞增子串、三角剖分、記憶化dp 6.博弈類演算法。博弈樹,二進位法等。 7.最大團,最大獨立集。 8.判斷點在多邊形內。 9. 差分約束系統. 10. 雙向廣度搜尋、A*演算法,最小耗散優先. 相關的知識 圖論 路徑問題 0/1邊權最短路徑 BFS 非負邊權最短路徑(Dijkstra) 可以用Dijkstra解決問題的特徵 負邊權最短路徑 Bellman-Ford Bellman-Ford的Yen-氏最佳化 差分約束系統 Floyd 廣義路徑問題 傳遞閉包 極小極大距離 / 極大極小距離 Euler Path / Tour 圈套圈演算法 混合圖的 Euler Path / Tour Hamilton Path / Tour 特殊圖的Hamilton Path / Tour 構造 產生樹問題 最小產生樹 第k小產生樹 最優比率產生樹 0/1分數規劃 度限制產生樹 連通性問題 強大的DFS演算法 無向圖連通性 割點 割邊 二連通分支 有向圖連通性 強連通分支 2-SAT 最小點基 有向非循環圖 拓撲排序 有向非循環圖與動態規劃的關係 二分圖匹配問題 一般圖問題與二分圖問題的轉換思路 最大匹配 有向圖的最小路徑覆蓋 0 / 1矩陣的最小覆蓋 完備匹配 最優匹配 穩定婚姻 網路流問題 網路流模型的簡單特徵和與線性規劃的關係 最大流最小割定理 最大流問題 有上下界的最大流問題 迴圈流 最小費用最大流 / 最大費用最大流 弦圖的性質和判定 組合數學 解決組合數學問題時常用的思想 逼近 遞推 / 動態規劃 機率問題 Polya定理 計算幾何 / 解析幾何 計算幾何的核心:叉積 / 面積 解析幾何的主力:複數 基本形 點 直線,線段 多邊形 凸多邊形 / 凸包 凸包演算法的引進,卷包裹法 Graham掃描法 水平序的引進,共線凸包的補丁 完美凸包演算法 相關判定 兩直線相交 兩線段相交 點在手繪多邊形內的判定 點在凸多邊形內的判定 經典問題 最小外接圓 近似O(n)的最小外接圓演算法 點集直徑 旋轉卡殼,對踵點 多邊形的三角剖分 數學 / 數論 最大公約數 Euclid演算法 擴充的Euclid演算法 同餘方程 / 二元一次不定方程 同餘方程組 線性方程組 高斯消元法 解mod 2域上的線性方程組 整係數方程組的精確......餘下全文>>
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.