Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

最优情况有多种,输出顺序不重要吧

Posted by njufz at 2013-09-13 15:24:43 on Problem 1033
// POJ_1033.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int N, K;
bool flag[10001] = { 0 };
bool optimal = false;

void from_to(int first, int from, int to, int *task){
	if(to == first){
		for (int i = N; i != 0; --i){
			if (!flag[i]){
				cout << from << " " << i << endl;
				flag[i] = true;
				flag[from] = false;		
				task[from] = 0;
				task[i] = to;
				break;
			}
		}
	}
	else if(flag[to]){
		int temp_to = task[to];
		from_to(first, to, temp_to, task);
		cout << from << " " << to << endl;
		task[from] = 0;
		flag[from] = false;
	}
	else{
		cout << from << " " << to << endl;
		task[from] = 0;
		flag[from] = false;
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	cin >> N >> K; 
	int *task = new int[N+1];
	for (int i = 0; i != N + 1; ++i)
		task[i] = 0;
	int to_c = 1;
	for (int i = 0; i != K; ++i){
		int S;
		cin >> S;
		for (int j = 0; j != S; ++j){
			int c;
			cin >> c;
			flag[c] = true;
			task[c] = to_c;
			++to_c;
		}
	}
	for (int i = 1; i != N + 1; ++i){
		if (task[i] != 0){
			if (task[i] == i)
				continue;
			else if (!flag[task[i]]){
				flag[task[i]] = true;
				flag[i] = false;
				cout << i << " " << task[i] << endl;
				task[i] = 0;
				optimal = true;
			}
			else{
				from_to(i, i, task[i], task);
				optimal = true;
			}
		}
	}
	if (!optimal)
		cout << "No optimization needed" << endl;
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator