UVA 1405 The Great Wall Game(最佳二分图匹配)
来源:互联网 发布:中智软件开发有限公司 编辑:程序博客网 时间:2024/06/06 02:40
一开始理所当然的认为横排和纵排应该是横坐标和纵坐标和的平均值,但是WA了好几次。检查了一下,好像也就这么一个可疑点,算算时间复杂度也不会T,就直接暴力了,然后就过了= =。
#include<iostream>using namespace std;const int maxn=300+5;const int inf=(1<<30);int w[maxn][maxn],n;int lx[maxn],ly[maxn];int Left[maxn];bool s[maxn],t[maxn];int id[maxn][maxn];struct point{int x,y;}p[maxn];bool match(int i){ s[i]=true; for(int j=1;j<=n;j++) if(lx[i]+ly[j]==w[i][j]&&!t[j]){ t[j]=true; if(!Left[j]||match(Left[j])){ Left[j]=i; return true; } } return false;}void update(){ int a=(1<<30); for(int i=1;i<=n;i++) if(s[i]) for(int j=1;j<=n;j++) if(!t[j]) a=min(a,lx[i]+ly[j]-w[i][j]); for(int i=1;i<=n;i++){ if(s[i]) lx[i]-=a; if(t[i]) ly[i]+=a; }}void KM(){ for(int i=1;i<=n;i++){ Left[i]=lx[i]=ly[i]=0; for(int j=1;j<=n;j++) lx[i]=max(lx[i],w[i][j]); } for(int i=1;i<=n;i++){ for(;;) { for(int j=1;j<=n;j++) s[j]=t[j]=0; if(match(i)) break;else update(); } }}void clear() {for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) w[i][j]=-inf;}int main(){ int kase=0; while(scanf("%d",&n)&&n) { for(int i=1;i<=n;i++){ scanf("%d%d",&p[i].x,&p[i].y); // cout<<p[i].x<<" "<<p[i].y<<endl; } int ans=inf,tmp=0; //横排和纵排 for(int i=1;i<=n;i++){ clear(); for(int k=1;k<=n;k++){ int x=p[k].x,y=p[k].y; for(int j=1;j<=n;j++){ w[k][j]=-(abs(x-i)+abs(y-j)); } } KM(); int tmp=0; for(int k=1;k<=n;k++) tmp-=w[Left[k]][k]; ans=min(ans,tmp); clear(); for(int k=1;k<=n;k++){ int x=p[k].x,y=p[k].y; // cout<<x<<" "<<y<<endl; for(int j=1;j<=n;j++){ w[k][j]=-(abs(x-j)+abs(y-i)); // cout<<k<<" "<<x<<" "<<y<<" "<<i<<" "<<j<<" "<<w[k][j]<<endl; } } KM(); tmp=0; for(int k=1;k<=n;k++) tmp-=w[Left[k]][k]; ans=min(ans,tmp); } // for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) w[i][j]=-inf; tmp=0; for(int i=1;i<=n;i++){ int xx=p[i].x,yy=p[i].y; for(int j=1;j<=n;j++){ w[i][j]=-(abs(xx-j)+abs(yy-j)); } } KM(); for(int i=1;i<=n;i++) tmp-=w[Left[i]][i]; ans=min(ans,tmp); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) w[i][j]=-inf; tmp=0; for(int i=1;i<=n;i++){ int xx=p[i].x,yy=p[i].y; for(int j=1;j<=n;j++){ w[i][j]=-(abs(xx-j)+abs(yy-(n-j+1))); } } KM(); for(int i=1;i<=n;i++) tmp-=w[Left[i]][i]; ans=min(ans,tmp); // if(kase++) printf("\n"); printf("Board %d: %d moves required.\n",++kase,ans); printf("\n"); } return 0;}
0 0
- UVA 1405 The Great Wall Game(最佳二分图匹配)
- UVA - 1045 The Great Wall Game(二分图最佳完美匹配)
- uva 1045 - The Great Wall Game(二分图匹配)
- LA 3276 The Great Wall Game(二分图最佳完美匹配)
- UVA 1045 - The Great Wall Game(二分图完美匹配)
- (beginer) 最小权匹配 UVA 1045 - The Great Wall Game
- Uva-1045-The Great Wall Game
- UVA 1045 The Great Wall Game
- uva 1045 - The Great Wall Game(KM)
- Fixing the Great Wall UVA
- Fixing the Great Wall UVA
- Uva-1336-Fixing the Great Wall
- UVa 1336 Fixing the Great Wall
- uva 1336 - Fixing the Great Wall
- UVA 1336 Fixing the Great Wall
- Fixing the Great Wall
- Fixing the Great Wall
- UVA 1336 Fixing the Great Wall(区间DP)
- Effective C++读书笔记之尽可能使用const
- ubuntu 14.04 搭建 nfs
- C++中命名空间的意义和用法
- jackson 框架 与 json-lib框架
- Linux进程间通信——pipe应用实例
- UVA 1405 The Great Wall Game(最佳二分图匹配)
- sql 创建表、删除表、 增加字段、删除字段等操作
- 设计模式(九)——组合模式
- Objective-c - 多个对象的内存管理之三:为某个属性反复赋值同一个对象
- bootstrap table使用总结
- eclipse中servers配置tomcat出现apr错误解决方案
- Activity生命周期的详解
- Leetcode——30. Substring with Concatenation of All Words
- Apache Flume 1.7深入浅出,快速入门