From Evernote: |
uva 670 |
最大流問題
[sourcecode language="cpp"]
//============================================================================
// Name : The dog task.cpp
// Date : 2013 2013/2/6 下午8:30:23
// 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 305
#define PII pair<int,int>
#define oo INT_MAX
#define PB push_back
#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 edge{
int cap,to;
}etmp;
vector<edge> mz[M];
vector<int> ans;
int match[M];
int q[99999999],front,rear;
int n,m;
int masx[M],masy[M];
int intx[M],inty[M];
double getdis(int x1,int y1,int x2,int y2){
return sqrt(1.0*(x1-x2)*(x1-x2)+1.0*(y1-y2)*(y1-y2));
}
void addpath(int x,int y){
etmp.cap=1;
etmp.to=y;
mz[x].PB(etmp);
etmp.to=x;
etmp.cap=0;
mz[y].PB(etmp);
}
int pflow[M],flow[M][M],father[M];
void ed(){
Set(flow,0);
int maxflow=0;
Set(match,-1);
while(1){
Set(pflow,0);
pflow[n+m]=oo;
front=rear=0;
q[front++]=n+m;
while(front>rear){
int u=q[rear++];
FOR(it,mz[u]){
int cap=(*it).cap,v=(*it).to;
if(!pflow[v]&&cap-flow[u][v]){
pflow[v]=min(pflow[u],cap-flow[u][v]);
father[v]=u;
q[front++]=v;
}
}
}
if(!pflow[n+m+1])break;
maxflow+=pflow[n+m+1];
for(int i=n+m+1;i!=n+m;i=father[i]){
flow[father[i]][i]+=pflow[n+m+1];
flow[i][father[i]]-=pflow[n+m+1];
if(i!=n+m+1&&father[i]!=n+m)match[father[i]]=i;
if(i!=n+m+1&&father[i]!=n+m)match[i]=father[i];
}
// for(int i=0;i<=10;i++){
//// debug(">>>i%d match%d\n",i,match[i]);
// }
// Pln();
}
printf("%d\n",n+maxflow);
printf("%d %d",masx[0],masy[0]);
if(match[0]!=-1)printf(" %d %d",intx[match[0]-n],inty[match[0]-n]);
for(int i=1;i<n;i++){
printf(" %d %d",masx[i],masy[i]);
if(match[i]!=-1)printf(" %d %d",intx[match[i]-n],inty[match[i]-n]);
}
Pln();
}
int main() {
ios_base::sync_with_stdio(0);
int test;
scanf("%d",&test);
while(test--){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d%d",&masx[i],&masy[i]);
for(int i=0;i<m;i++)
scanf("%d%d",&intx[i],&inty[i]);
double mist,toin;
for(int i=0;i<n-1;i++){
mist=getdis(masx[i],masy[i],masx[i+1],masy[i+1]);
for(int j=0;j<m;j++){
toin=getdis(masx[i],masy[i],intx[j],inty[j]);
toin+=getdis(masx[i+1],masy[i+1],intx[j],inty[j]);
// debug("%d %d %.5f %.5f x1%d y1%d x2%d y2%d\n",i,j+n,mist*2,toin,masx[i],masy[i],masx[i+1],masy[i+1]);
if(xcdy(toin,mist*2)){
addpath(n+m,i);
addpath(i,j+n);
addpath(j+n,n+m+1);
}
}
}
ed();
if(test)Pln();
for(int i=0;i<=n+m+2;i++)mz[i].clear();
}
}
[/sourcecode]
留言
張貼留言