Red Huang

Red Huang

uva 11506

來自 Evernote:#

11506#

員工報仇剪斷 BOSS 的線路

M = 機器
W = 線路
boss id=1
central server id=M
c = 摧毀機器的成本
d = 摧毀線路的成本
所有線路都是雙向的!!

輸入:
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
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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 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 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::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]

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。