Ants

Language:Default
Ants
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7975 Accepted: 2517 Special Judge

Description

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself.

Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants travel from their colony to their feeding places and back using chemically tagged routes. The routes cannot intersect each other or ants will get confused and get to the wrong colony or tree, thus spurring a war between colonies.

Bill would like to connect each ant colony to a single apple tree so that all n routes are non-intersecting straight lines. In this problem such connection is always possible. Your task is to write a program that finds such connection.

On this picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles. One possible connection is denoted by lines.

Input

The first line of the input file contains a single integer number n (1 ≤ n ≤ 100) — the number of ant colonies and apple trees. It is followed by n lines describing n ant colonies, followed by n lines describing n apple trees. Each ant colony and apple tree is described by a pair of integer coordinates x and y (−10 000 ≤ x, y ≤ 10 000) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No three points are on the same line.

Output

Write to the output file n lines with one integer number on each line. The number written on i-th line denotes the number (from 1 to n) of the apple tree that is connected to the i-th ant colony.

Sample Input

  1. 5
  2. -42 58
  3. 44 86
  4. 7 28
  5. 99 34
  6. -13 -59
  7. -47 -44
  8. 86 74
  9. 68 -75
  10. -68 60
  11. 99 -60

Sample Output

  1. 4
  2. 2
  3. 1
  4. 5
  5. 3

Source

在坐标系中有N只蚂蚁,N棵苹果树,给你蚂蚁和苹果树的坐标。让每只蚂蚁去一棵苹果树,一棵苹果树对应一只蚂蚁。这样就有N条直线路线,问:怎样分配,才能使总路程和最小,且N条线不相交。

题解

线段不相交,等价于让所有线段长度之和最小。因为相交的线段一定可以通过调整转化为不相交且长度较小的线段。

那么求的就是最小权完美匹配。将边权取负,跑KM算法即可。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cmath>
  4. #define rg register
  5. #define il inline
  6. #define co const
  7. template<class T>il T read(){
  8. rg T data=0,w=1;rg char ch=getchar();
  9. for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
  10. for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
  11. return data*w;
  12. }
  13. template<class T>il T read(rg T&x) {return x=read<T>();}
  14. typedef long long ll;
  15. using namespace std;
  16. co int N=101;
  17. int a[N],b[N],c[N],d[N];
  18. double w[N][N];
  19. double la[N],lb[N]; // top label
  20. bool va[N],vb[N]; // in tree
  21. int match[N],ans[N];
  22. int n;
  23. double upd[N],delta;
  24. bool dfs(int x){
  25. va[x]=1;
  26. for(int y=1;y<=n;++y)if(!vb[y]){
  27. if(abs(la[x]+lb[y]-w[x][y])<1e-8){ // equal subgraph
  28. vb[y]=1;
  29. if(!match[y]||dfs(match[y])){
  30. match[y]=x;
  31. return 1;
  32. }
  33. }
  34. else upd[y]=min(upd[y],la[x]+lb[y]-w[x][y]);
  35. }
  36. return 0;
  37. }
  38. void KM(){
  39. for(int i=1;i<=n;++i){
  40. la[i]=1e-10,lb[i]=0;
  41. for(int j=1;j<=n;++j) la[i]=max(la[i],w[i][j]);
  42. }
  43. for(int i=1;i<=n;++i)while(1){ // until found
  44. fill(va+1,va+n+1,0),fill(vb+1,vb+n+1,0);
  45. delta=1e10,fill(upd+1,upd+n+1,1e10);
  46. if(dfs(i)) break;
  47. for(int j=1;j<=n;++j)
  48. if(!vb[j]) delta=min(delta,upd[j]);
  49. for(int j=1;j<=n;++j){
  50. if(va[j]) la[j]-=delta;
  51. if(vb[j]) lb[j]+=delta;
  52. }
  53. }
  54. }
  55. int main(){
  56. read(n);
  57. for(int i=1;i<=n;++i) read(a[i]),read(b[i]);
  58. for(int i=1;i<=n;++i) read(c[i]),read(d[i]);
  59. for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)
  60. w[i][j]=-sqrt(pow((double)a[i]-c[j],2)+pow((double)b[i]-d[j],2));
  61. KM();
  62. for(int i=1;i<=n;++i) ans[match[i]]=i;
  63. for(int i=1;i<=n;++i) printf("%d\n",ans[i]);
  64. return 0;
  65. }

Going Home

Language:Default
Going Home
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 26913 Accepted: 13425

Description

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.



Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a '.' means an empty space, an 'H' represents a house on that point, and am 'm' indicates there is a little man on that point.



You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

  1. 2 2
  2. .m
  3. H.
  4. 5 5
  5. HH..m
  6. .....
  7. .....
  8. .....
  9. mm..H
  10. 7 8
  11. ...H....
  12. ...H....
  13. ...H....
  14. mmmHmmmm
  15. ...H....
  16. ...H....
  17. ...H....
  18. 0 0

Sample Output

  1. 2
  2. 10
  3. 28

Source

m表示人,H表示房子,一个人只能进一个房子,一个房子也只能进去一个人,房子数等于人数,现在要让所有人进入房子,求所有人都进房子最短的路径。

题解

这题更无脑,直接连边求最优完美匹配就行了。费用流解决。

  1. #include<iostream>
  2. #include<cstring>
  3. #include<queue>
  4. #include<cmath>
  5. #include<bitset>
  6. #define rg register
  7. #define il inline
  8. #define co const
  9. template<class T>il T read(){
  10. rg T data=0,w=1;rg char ch=getchar();
  11. for(;!isdigit(ch);ch=getchar())if(ch=='-') w=-w;
  12. for(;isdigit(ch);ch=getchar()) data=data*10+ch-'0';
  13. return data*w;
  14. }
  15. template<class T>il T read(rg T&x) {return x=read<T>();}
  16. typedef long long ll;
  17. using namespace std;
  18. typedef pair<int,int> pii;
  19. #define x first
  20. #define y second
  21. co int N=202;
  22. int n,m,ta,tb,t,d[N],now[N],pre[N],ans;
  23. char s[N][N];
  24. pii a[N],b[N];
  25. int head[N],edge[N*N],leng[N*N],cost[N*N],next[N*N],tot;
  26. bitset<N> v;
  27. il int dis(int i,int j){
  28. return abs(a[i].x-b[j].x)+abs(a[i].y-b[j].y);
  29. }
  30. il void add(int x,int y,int z,int w){
  31. edge[++tot]=y,leng[tot]=z,cost[tot]=w,next[tot]=head[x],head[x]=tot;
  32. }
  33. bool spfa(){
  34. memset(d,0x3f,sizeof d),d[0]=0;
  35. queue<int> q;q.push(0);
  36. v.reset(),v[0]=1;
  37. now[0]=0x7fffffff;
  38. while(q.size()){
  39. int x=q.front();q.pop();
  40. v[x]=0;
  41. for(int i=head[x];i;i=next[i]){
  42. int y=edge[i],z=leng[i],w=cost[i];
  43. if(!z||d[y]<=d[x]+w) continue;
  44. d[y]=d[x]+w,now[y]=min(now[x],z),pre[y]=i;
  45. if(!v[y]) q.push(y),v[y]=1;
  46. }
  47. }
  48. return d[t<<1|1]!=0x3f3f3f3f;
  49. }
  50. void upd(){
  51. ans+=d[t<<1|1]*now[t<<1|1];
  52. int x=t<<1|1;
  53. while(x){
  54. int i=pre[x];
  55. leng[i]-=now[t],leng[i^1]+=now[t];
  56. x=edge[i^1];
  57. }
  58. }
  59. void Going_Home(){
  60. ans=ta=tb=0;
  61. for(int i=1;i<=n;++i) scanf("%s",s[i]+1);
  62. for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){
  63. if(s[i][j]=='H') a[++ta]=pii(i,j);
  64. else if(s[i][j]=='m') b[++tb]=pii(i,j);
  65. }
  66. t=ta,tot=1;
  67. for(int i=0;i<=(t<<1|1);++i) head[i]=0;
  68. for(int i=1;i<=t;++i)for(int j=1;j<=t;++j){
  69. int k=dis(i,j);
  70. add(i,j+t,1,k),add(j+t,i,0,-k);
  71. }
  72. for(int i=1;i<=t;++i){
  73. add(0,i,1,0),add(i,0,0,0);
  74. add(i+t,t<<1|1,1,0),add(t<<1|1,i+t,0,0);
  75. }
  76. while(spfa()) upd();
  77. printf("%d\n",ans);
  78. }
  79. int main(){
  80. while(read(n)|read(m)) Going_Home();
  81. return 0;
  82. }

POJ3565 Ants 和 POJ2195 Going Home的更多相关文章

  1. [poj3565]Ants

    [poj3565]Ants 标签(空格分隔):二分图 描述 Young naturalist Bill studies ants in school. His ants feed on plant-l ...

  2. poj3565 Ants km算法求最小权完美匹配,浮点权值

    /** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求 ...

  3. ACM学习历程—POJ3565 Ants(最佳匹配KM算法)

    Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees ...

  4. POJ3565 Ants (不相交线)

    那请告诉我 A - D  B - C  和  A - C  B - D 那个的和小 显然是A - C  B - D  (可以根据四边形 对角线大于对边之和) 然后 求的答案是不是就一定是不相交的 就是 ...

  5. [poj3565] Ants (二分图带权匹配)

    传送门 Description 年轻自然主义者比尔在学校研究蚂蚁. 他的蚂蚁以苹果树上苹果为食. 每个蚁群都需要自己的苹果树来养活自己. 比尔有一张坐标为 n 个蚁群和 n 棵苹果树的地图. 他知道蚂 ...

  6. 【POJ3565】ANTS KM算法

    [POJ3565]ANTS 题意:平面上有2*n个点,N白N黑.为每个白点找一个黑点与之连边,最后所有边不交叉.求一种方案. 题解:KM算法真是一个神奇的算法,虽然感觉KM能做的题用费用流都能做~ 本 ...

  7. 使用ANTS Performance Profiler&ANTS Memory Profiler工具分析IIS进程内存和CPU占用过高问题

    一.前言 最近一段时间,网站经常出现两个问题: 1.内存占用率一点点增高,直到将服务器内存占满. 2.访问某个页面时,页面响应过慢,CPU居高不下. 初步判断内存一点点增多可能是因为有未释放的资源一直 ...

  8. poj1852 Ants ——想法题、水题

    求最短时间和最长时间. 当两个蚂蚁相遇的时候,可以看做两个蚂蚁穿过,对结果没有影响.O(N)的复杂度 c++版: #include <cstdio> #define min(a, b) ( ...

  9. Uva10881 Piotr's Ants

    蚂蚁相撞会各自回头.←可以等效成对穿而过,这样移动距离就很好算了. 末状态蚂蚁的顺序和初状态其实是相同的. 那么剩下的就是记录每只蚂蚁的标号,模拟即可. /*by SilverN*/ #include ...

随机推荐

  1. Netty实现java多线程Post请求解析(Map参数类型)—SKY

    netty解析Post的键值对 解析时必须加上一个方法,ch.pipeline().addLast(new HttpObjectAggregator(2048)); 放在自己的Handel前面. ht ...

  2. [Android]豆瓣FM离线数据

    离线目录结构: /sdcard/Android/data/com.douban.radio下 ./cache/fileCaches: 离线音乐歌词(lyric) ./cache/images: 离线音 ...

  3. 【python】-- web开发之DOM

    DOM 文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口.它给文档提供了一种结构化的表示方法,可以改变文档的内容和呈现方式.我们最为关心的是, ...

  4. Java编程中的一些常见问题汇总

    转载自  http://macrochen.iteye.com/blog/1393502 每天在写Java程序,其实里面有一些细节大家可能没怎么注意,这不,有人总结了一个我们编程中常见的问题.虽然一般 ...

  5. MySQL5.7.26 忘记Root密码小计

    以前直接修改mysql.user就ok了,现在不行了,正好虚拟机MySQL的root密码忘记了,就简单记录下:(本方法不适合互联网线上项目,除非你不在意这段时间的损失) PS:以UbuntuServe ...

  6. Unknown Entity namespace alias 'BaseMemberBundle'.

    $em = $this->getDoctrine()->getManager('member');//要记得写上member $repo = $em->getRepository(' ...

  7. python 安装anaconda, numpy, pandas, matplotlib 等

    如果没安装anaconda,则这样安装这些库: pip install numpy pip install pandas pip install matplotlib sudo apt-get ins ...

  8. jQuery对象转成DOM对象:

    jQuery对象转成DOM对象: 两种转换方式将一个jQuery对象转换成DOM对象:[index]和.get(index); (1)jQuery对象是一个数据对象,可以通过[index]的方法,来得 ...

  9. CVPR 2018paper: DeepDefense: Training Deep Neural Networks with Improved Robustness第一讲

    前言:好久不见了,最近一直瞎忙活,博客好久都没有更新了,表示道歉.希望大家在新的一年中工作顺利,学业进步,共勉! 今天我们介绍深度神经网络的缺点:无论模型有多深,无论是卷积还是RNN,都有的问题:以图 ...

  10. 动态添加select的option

    动态给select标签添加option,结合前人经验以及自己经验,现在总结三种方法供大家参考,一起交流学习! 首先是定义的select元素: //根据ID获得select元素var mySelect ...