POJ 3436-ACM Computer Factory(最大流輸出路徑-Edmond-Karp演算法)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 7445 | Accepted: 2664 | Special Judge |
Description
As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.
Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.
Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.
Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of P numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn't matter.
Output specification describes the result of the operation, and is a set of P numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.
The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.
After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.
As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.
Input
Input file contains integers P N, then N descriptions of the machines. The description of ith machine is represented as by 2 P + 1 integers Qi Si,1 Si,2...Si,P Di,1Di,2...Di,P, where Qi specifies performance, Si,j — input specification for part j, Di,k — output specification for part k.
Constraints
1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Qi ≤ 10000
Output
Output the maximum possible overall performance, then M — number of connections that must be made, then M descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, where W is the number of computers delivered from A to B per hour.
If several solutions exist, output any of them.
Sample Input
Sample input 1 3 4 15 0 0 0 0 1 0 10 0 0 0 0 1 1 30 0 1 2 1 1 1 3 0 2 1 1 1 1 Sample input 2 3 5 5 0 0 0 0 1 0 100 0 1 0 1 0 1 3 0 1 0 1 1 0 1 1 0 1 1 1 0 300 1 1 2 1 1 1 Sample input 3 2 2 100 0 0 1 0 200 0 1 1 1
Sample Output
Sample output 1 25 2 1 3 15 2 3 10 Sample output 2 4 5 1 3 3 3 5 3 1 2 1 2 4 1 4 5 1 Sample output 3 0 0
Hint
Source
題目意思:
解題思路:
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1010
struct Node
{
int in[11];
int out[11];
int q;
} s[MAXN];
struct node
{
int from,to,cap;
node(int a,int b,int c):from(a),to(b),cap(c) {}
};
vector<node> res;//記錄網路流中機器之間的流量
int temp[MAXN][MAXN];
int capacity[MAXN][MAXN]; //記錄殘留網路的容量
int flow[MAXN]; //標記從源點到當前節點實際還剩多少流量可用
int pre[MAXN]; //標記在這條路徑上當前節點的前驅,同時標記該節點是否在佇列中
int n,p; //總結點數、組成部分數
queue<int> myqueue;
int BFS(int src,int des)
{
int i;
while(!myqueue.empty()) //佇列清空
myqueue.pop();
for(i=0; i<=n+1; ++i)
pre[i]=-1;
pre[src]=0;
flow[src]= INF;
myqueue.push(src);
while(!myqueue.empty())
{
int index = myqueue.front();
myqueue.pop();
if(index == des) //找到了增廣路徑
break;
for(i=0; i<=n+1; ++i)
{
if(i!=src && capacity[index][i]&& pre[i]==-1)
{
pre[i] = index; //記錄前驅
flow[i] = min(capacity[index][i],flow[index]); //關鍵:迭代的找到增量
myqueue.push(i);
}
}
}
if(pre[des]==-1) //殘留圖中不再存在增廣路徑
return -1;
else
return flow[des];
}
int maxFlow(int src,int des)
{
int increasement= 0;
int sumflow = 0;
while((increasement=BFS(src,des))!=-1)
{
int k = des; //利用前驅尋找路徑
while(k!=src)
{
int last = pre[k];
capacity[last][k] -= increasement; //改變正向邊的容量
capacity[k][last] += increasement; //改變反向邊的容量
k = last;
}
sumflow += increasement;
}
return sumflow;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int i,j;
while(cin>>p>>n)
{
int t=n;
n*=2;
memset(capacity,0,sizeof(capacity));
memset(flow,0,sizeof(flow));
for(i=1; i<=t; ++i)
{
cin>>s[i].q;
capacity[i][i+t]=s[i].q;//
bool b0=true,b1=true;
for(j=0; j<p; ++j)
{
cin>>s[i].in[j];
if(s[i].in[j]==1) b0=false;//存在1
}
for(j=0; j<p; ++j)
{
cin>>s[i].out[j];
if(!s[i].out[j]) b1=false;//存在0
}
if(b0) capacity[0][i]=INF;//超級源點0
if(b1) capacity[i+t][n+1]=INF;//超級匯點n+1
}
bool flag=true;
for(i=1; i<=t; ++i)
for(j=1; j<=t; ++j)
if(i!=j)
{
flag=true;
for(int k=0; k<p; ++k)
if(s[i].out[k]+s[j].in[k]==1)
{
flag=false;
break;
}
if(flag)
capacity[i+t][j]=INF;//機器之間
}
memcpy(temp,capacity,sizeof(temp));//複製capacity陣列到temp
cout<<maxFlow(0,n+1)<<" ";
for(int i=1; i<=t; i++)
{
for(int j=1; j<=t; j++)
{
if(i!=j)
if(capacity[t+i][j]<temp[t+i][j])//流量有減少說明需要使用該機器
res.push_back(node(i,j,temp[t+i][j]-capacity[t+i][j]));
}
}
cout<<res.size()<<endl;
for(int i=0; i<res.size(); i++)
cout<<res[i].from<<" "<<res[i].to<<" "<<res[i].cap<<endl;
res.clear();
}
return 0;
}
/*
Sample input 1
3 4
15 0 0 0 0 1 0
10 0 0 0 0 1 1
30 0 1 2 1 1 1
3 0 2 1 1 1 1
Sample input 2
3 5
5 0 0 0 0 1 0
100 0 1 0 1 0 1
3 0 1 0 1 1 0
1 1 0 1 1 1 0
300 1 1 2 1 1 1
Sample input 3
2 2
100 0 0 1 0
200 0 1 1 1
*/
相關文章
- POJ 1459-Power Network(最大流-Edmond-Karp演算法)演算法
- POJ 1273-Drainage Ditches(最大流-Edmond-Karp演算法/模板)AI演算法
- 動態規劃如何輸出路徑?動態規劃
- poj 3436 最大流的增廣路演算法演算法
- VC6修改lib檔案的輸出路徑
- POJ 3469-Dual Core CPU(Dinic 最大流/最小割演算法)演算法
- POJ 2195-Going Home(KM演算法/最小費用最大流演算法)Go演算法
- poj1087 網路最大流
- POJ 3308 Paratroopers 最小割、最大流OOP
- poj 3764 最長異或路徑(二進位制trie樹)
- POJ 2947 Widget Factory(取模的高斯消元)
- POJ 2195 Going Home 最小費用最大流Go
- POJ 2195 Going Home (最小費用最大流)Go
- 最大流 dinic演算法演算法
- MySQL資料庫中的日誌檔案---(4)配置日誌檔案輸出路徑MySql資料庫
- POJ1273 Drainage Ditches【網路流 最大流】AI
- 最大流 EdmondsKarp演算法演算法
- 網路最大流演算法演算法
- 最短路徑--dijkstra演算法、弗洛伊德(Floyd)演算法(帶路徑輸出)演算法
- POJ 基本演算法演算法
- 網路最大流 Dinic演算法演算法
- 經典演算法-最大流問題演算法
- POJ 1141 Brackets Sequence(記錄路徑的dp)Racket
- POJ 1149-PIGS(Ford-Fulkerson 標號法求網路最大流)
- A. Build a ComputerUI
- 最短路徑演算法演算法
- git internal for computer scientistsGit
- POJ 3373 Changing Digits(記錄路徑的dp)Git
- 687. 最長同值路徑
- 最短路徑(Floyd演算法)演算法
- Djikstra最短路徑演算法演算法
- 演算法——路徑問題演算法
- poj2594Treasure Exploration【最小路徑覆蓋+floyd傳遞閉包】
- (UVA - 208)Firetruck(路徑輸出問題,回溯+並查集/floyd演算法+dfs)並查集演算法
- 路徑規劃: 淺談路徑規劃演算法演算法
- P4551 最長異或路徑
- 【GeoScene】一、建立、釋出路網服務,並在程式碼中測試最短路徑分析
- 最簡單的Qt程式:根據使用者所輸入圓半徑計算圓面積QT