UVa670 - The dog task(最大二分匹配)

The dog task 

Hunter Bob often walks with his dog Ralph. Bob walks with a constant speed and his route is a polygonal line (possibly self-intersecting) whose vertices are specified byN pairs of integers (Xi, Yi) - their Cartesian coordinates.


Ralph walks on his own way but always meets his master at the specified Npoints. The dog starts his journey simultaneously with Bob at the point (X1,Y1) and finishes it also simultaneously with Bob at the point (XN , YN).


Ralph can travel at a speed that is up to two times greater than his master's speed. While Bob travels in a straight line from onepoint to another the cheerful dog seeks trees, bushes, hummocks and all other kinds ofinteresting places of the local landscapewhich are specified by M pairs of integers (X'j,Y'j). However, after leaving his master at the point (Xi ,Yi) (where $1 \le i < N$)the dog visits at most one interesting place before meeting his master again at the point (Xi+1 , Yi+1).


Your task is to find the dog's route, which meets the above requirements and allows him to visit the maximal possible numberof interesting places. The answer should be presented as a polygonal line that represents Ralph's route. The vertices of thisroute should be all points (Xi , Yi) and the maximal number ofinteresting places (X'j,Y'jX). The latter should be visited (i.e.listed in the route description) at most once.


An example of Bob's route (solid line), a set of interesting places (dots) and one of the best Ralph's routes (dotted line) arepresented in the following picture:

Input 

The first line of the input is an integer L, then a blank line followed by L datasets. There is a blank line between datasets.

The first line of each dataset contains two integers N and M, separatedby a space ($2 \le N \le 100, 0 \le M \le 100$). The secondline contains N pairs of integers $X_1, Y_1, \dots, X_N, Y_N$,separated byspaces, that represent Bob's route. The third line contains M pairs ofintegers $X'_1, Y'_1, \dots, X'_M, Y'_M$,separated by spaces, that represent interesting places.


All points in the input file are different and their coordinates are integersnot greater than 1000 by the absolute value.

Output 

The first line of each dataset should contain the single integer K - thenumber of vertices of the best dog's route. The second line should contain Kpairs of coordinates $X''_1, Y''_1, \dots, X''_K, Y''_K$,separated by spaces, that represent this route. If there areseveral such routes, then you may write any of them.

Print a blank line between datasets.

Sample Input 

1

4 5
1 4 5 7 5 2 -2 4
-4 -2 3 9 1 2 -1 3 8 -3

Sample Output 

6
1 4 3 9 5 7 5 2 1 2 -2 4

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main 
{
	public static final boolean DEBUG = false;
	public static final double EPS = 1e-6;
	public StreamTokenizer tokenizer;
	public BufferedReader cin;
	public PrintWriter cout;
	public int[] leftx, lefty, rightx, righty;
	public int[] match;
	public int n, m;
	public int[][] g;
	public boolean[] vis;
	public int ans;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("d:\\OJ\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String next()
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)tokenizer.nval);
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
			else return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public double dis(int x1, int y1, int x2, int y2)
	{
		double x = x1 - x2;
		double y = y1 - y2;
		
		return Math.sqrt(x * x + y * y);
	}
	
	public boolean input() 
	{
		n = Integer.parseInt(next());
		m = Integer.parseInt(next());
		
		rightx = new int[n];
		righty = new int[n];
		leftx = new int[m];
		lefty = new int[m];
		
		for (int i = 0; i < n; i++) {
			rightx[i] = Integer.parseInt(next());
			righty[i] = Integer.parseInt(next());
		}
		
		
		
		
		for (int i = 0; i < m; i++) {
			leftx[i] = Integer.parseInt(next());
			lefty[i] = Integer.parseInt(next());
		}
		
		g = new int[m][n];
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n - 1; j++) {
				double d1 = dis(leftx[i], lefty[i], rightx[j], righty[j]);
				double d2 = dis(leftx[i], lefty[i], rightx[j + 1], righty[j + 1]);
				double d = dis(rightx[j], righty[j], rightx[j + 1], righty[j + 1]);
				if (d1 + d2 - 2 * d < EPS) g[i][j] = 1;
			}
		}
		
		return true;
	}

	public boolean find(int u)
	{
		for (int v = 0; v < n; v++) {
			if (g[u][v] != 0 && !vis[v]) {
				vis[v] = true;
				if (match[v] == -1 || find(match[v])) {
					match[v] = u;
					return true;
				}
			}
		}
		
		return false;
	}
	
	public void solve(int cas) 
	{
		vis = new boolean[n];
		ans = 0;
		match = new int[n];
		
		Arrays.fill(match, -1);
		
		for (int i = 0; i < m; i++) {
			Arrays.fill(vis, false);
			if (find(i)) ans++;
		}
		
		cout.println(ans + n);
		
		for (int i = 0; i < n; i++) {
			if (i == 0) cout.print(rightx[i] + " " + righty[i]);
			else cout.print(" " + rightx[i] + " " + righty[i]);
			int v = match[i];
			if (v != -1) cout.print(" " + leftx[v] + " " + lefty[v]);
		}
		cout.println();
		if (cas != 0) cout.println();
		cout.flush();
		
		
	}

	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		int t = Integer.parseInt(solver.next());
		while (t-- > 0) {
			solver.input();
			solver.solve(t);
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值