程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj 2565 Ants (KM+思維)

poj 2565 Ants (KM+思維)

日期:2017/1/21 14:38:08      編輯:C++入門知識

Ants Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4125 Accepted: 1258 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 000x, y10 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

5
-42 58
44 86
7 28
99 34
-13 -59
-47 -44
86 74
68 -75
-68 60
99 -60

Sample Output

4
2
1
5
3

Source

Northeastern Europe 2007

題意:

給你n個黑點和n個白點。叫你找出一種連點的方法使得每一白點連一個黑點。切連線不與其它連線相交。

思路:

看上去跟想幾何問題跟KM沒什麼關系。但確實是KM題。求連線的最短距離就行了。因為在連線距離最小的條件下。不會有兩條直線相交的情況。畫個圖就知道(三角形兩邊之和大於第三邊)。需要稍稍的轉換下。兩點間的價值為距離的負數。還有特別注意答案要求輸出每一個C對應的A。所以建邊時要注意left[i]代表什麼!鄙人就為這個WA了好幾發。TT。

詳細見代碼:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=1e-6;
const double PI=acos(-1.0);
const int maxn=110;
//typedef __int64 ll;
int le[maxn],n;
double lx[maxn],ly[maxn],slack[maxn],w[maxn][maxn],ax[maxn],ay[maxn],bx[maxn],by[maxn];
bool visx[maxn],visy[maxn];
double dist(double x1,double y1,double x2,double y2)
{
    double x=x1-x2,y=y1-y2;
    return sqrt(x*x+y*y);
}
bool match(int x)
{
    int y;
    double tp;
    visx[x]=true;
    for(y=1;y<=n;y++)
    {
        if(visy[y])
            continue;
        tp=lx[x]+ly[y]-w[x][y];
        if(tp

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved