跳到主要內容

uva 11506











From Evernote:



11506



員工報仇剪斷BOSS的線路

M=machines
W=wires
boss id=1
central server id=M
c=cost of destroying the machine
d=cost of destroying the wire
all wire are bidrectional!!

input:
4 4
3 5
2 2
1 2 3
1 3 3
2 4 1
3 4 3
4 4
3 2
2 2
1 2 3
1 3 3
2 4 1
3 4 3
0 0

遇到一個問題,點有權重,而且又是流的問題
GOOGLE找到一個方法,把點分成兩個點:入點跟出點
out->in
而自己的in到自己的out就是點的權值
本來TLE,發現好像是因為M設太大讓STL跑太久的緣故
結論AC

[sourcecode language="cpp"]
//============================================================================
// Name : Angry Programmer.cpp
// Date : 2013 2013/1/27 下午5:38:26
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 105
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)

using namespace std;
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
int m,w;
struct node{int to,cap;};
node ntmp;
vector<node> mz[M];
int flow[M][M];
int maxflow=0;
void addpath(int x,int y,int c){
ntmp.cap=c;
ntmp.to=y;
mz[x].PB(ntmp);
ntmp.cap=0;
ntmp.to=x;
mz[y].PB(ntmp);
}
void ed(){
// int front,rear;
// front=rear=maxflow=0;
maxflow=0;
queue<int> q;
Set(flow,0);
int pflow[M];
int father[M];
while(1){
Set(pflow,0);
// q[front++]=1;
q.push(1);
pflow[1]=oo;
while(!q.empty()){
// int u=q[rear++];
int u=q.front();q.pop();
for(vector<node>::iterator j=mz[u].begin();j!=mz[u].end();j++){
if(!pflow[(*j).to]&&(*j).cap-flow[u][(*j).to]>0){
q.push((*j).to);
// q[front++]=(*j).to;
pflow[(*j).to]=min(pflow[u],(*j).cap-flow[u][(*j).to]);
father[(*j).to]=u;
}
}
}
if(!pflow[m])break;
maxflow+=pflow[m];
for(int i=m;i!=1;i=father[i]){
flow[father[i]][i]+=pflow[m];
flow[i][father[i]]-=pflow[m];
}
}
printf("%d\n",maxflow);
}
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%d%d",&m,&w)&&m+w){
int x,y,c;
for(int i=0;i<m-2;i++){
scanf("%d%d",&x,&c);
addpath(x,x+m,c);
}
for(int i=0;i<w;i++){
scanf("%d%d%d",&x,&y,&c);
addpath(x+m,y,c);
addpath(y+m,x,c);
}
addpath(1,1+m,oo);
addpath(m,m<<1,oo);
ed();
for(int i=0;i<=(m<<1);i++)mz[i].clear();
}

}

[/sourcecode]

留言