跳到主要內容

uva 753








From Evernote:

uva 753



感覺上頗複雜的最大流

錯了 是超複雜..

[sourcecode language="cpp"]
//============================================================================
// Name : A Plug for UNIX.cpp
// Date : 2013 2013/2/27 下午6:24:59
// 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 405
#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__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
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);
}
struct node{
int u,c;
}ntmp;
vector<node> mz[M];
int n,m,k;
char plug[M][35];
char device[M][35];
char device_plug[M][35];
char adapter_from[M][35],adapter_to[M][35];
int q[M*M],front,rear;
void addpath(int x,int y,int c){
ntmp.u=y;
ntmp.c=c;
mz[x].PB(ntmp);
ntmp.u=x;
ntmp.c=0;
mz[y].PB(ntmp);
}
void init(){
for(int i=1;i<=m;i++)
addpath(0,i,1);
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
if(!strcmp(device_plug[i],plug[j])){
addpath(i,k+j+m,1);
// debug("1%d %d\n",i,j);
}
}
for(int j=1;j<=k;j++){
if(!strcmp(device_plug[i],adapter_to[j])){
// debug("%s %s\n",device_plug[i],adapter_to[j]);
addpath(i,j+m,oo);
// debug("2%d %d\n",i,j);
}
}
}
for(int i=1;i<=k;i++){
for(int j=1;j<=k;j++){
if(i!=j&&!strcmp(adapter_from[i],adapter_to[j])){
addpath(i+m,j+m,oo);
// debug("3%d %d\n",i,j);
}
}
for(int j=1;j<=n;j++){
if(!strcmp(adapter_from[i],plug[j])){
addpath(i+m,j+m+k,1);
// debug("4%d %d\n",i,j);
}
}
}
for(int i=1;i<=n;i++){
addpath(i+m+k,n+m+k+1,1);
}
}
void ed(){
int flow[M][M],pflow[M];
Set(flow,0);
int father[M];
int maxflow=0;
while(1){
Set(pflow,0);
pflow[0]=oo;
front=rear=0;
q[front++]=0;
while(front>rear){
int u=q[rear++];
FOR(it,mz[u]){
int v=(*it).u,c=(*it).c;

if(!pflow[v]&&c-flow[u][v]>0){
q[front++]=v;
pflow[v]=min(pflow[u],c-flow[u][v]);
father[v]=u;
}
}
}
if(!pflow[n+m+k+1])break;
maxflow+=pflow[n+m+k+1];
for(int i=n+m+k+1;i!=0;i=father[i]){
// debug("%d %d\n",father[i],i);
flow[father[i]][i]+=pflow[n+m+k+1];
flow[i][father[i]]-=pflow[n+m+k+1];
}
}
printf("%d\n",m-maxflow);
}
int main() {
ios_base::sync_with_stdio(0);
int test;
scanf("%d",&test);
while(test--){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%s",plug[i]);
scanf("%d",&m);
for(int j=1;j<=m;j++)
scanf("%s %s",device[j],device_plug[j]);
scanf("%d",&k);
for(int l=1;l<=k;l++){
scanf("%s %s",adapter_to[l],adapter_from[l]);
}
init();
ed();
if(test)Pln();
for(int i=0;i<=m+n+k+10;i++)mz[i].clear();
}

}

[/sourcecode]

留言