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 KatrineYang at 2016-06-15 15:28:08 on Problem 1041 and last updated at 2016-06-15 15:30:03
不暴力不会超时,TLE基本是因为死循环
//============================================================================
// Name        : main1041.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
#include <queue>

using namespace std;
class junc;

class street{
public:
	junc* j1;
	junc* j2;
	int num;
};

bool operator<(street a, street b){
	return a.num > b.num;
}

ostream& operator<<(ostream& out, street s){
	out << s.j1 << " " << s.j2 << " " << s.num << endl;
	return out;
}

class junc{
public:
	//int num;
	//junc* next;
	//int streetNum;
	priority_queue<street> qs;

};

class listNode{
public:
	listNode *next;
	junc *j;
	int streetNum;
	listNode(){
		next = NULL;
		j = NULL;
		streetNum = 0;
	}
};

ostream& operator<<(ostream& out, listNode& l){
	listNode *L = &l;
	int cnt = 0;
	while(L != NULL && cnt < 10){
		out << L << " ";
		L = L->next;
		cnt++;
	}
	out << endl;
	return out;
}

int Min(int a, int b){
	if(a < b) return a;
	return b;
}

int Max(int a, int b){
	if(a > b) return a;
	return b;
}

int main() {/*
	priority_queue<int,vector<int>,greater<int> > q;
	q.push(3); q.push(5); q.push(5); q.push(-9); q.push(16);
	for(int i = 0; i < 5; i++) {
		cout << q.top() << endl;
		q.pop();
	}*/
	while(1){
		int cnt = 0;
		int start;
		int maxi = 0;
		//int streetNum = 0;
		//int data[6000] = {0};
		junc junctions[2000];
		while(1){
			int j1, j2, str;
			cin >> j1 >> j2;
			if(j1==0 && j2==0){
				break;
			}
			cnt++;
			if(cnt == 1) start = Min(j1, j2);
			cin >> str;
			if(maxi < Max(j1, j2)) maxi = Max(j1, j2);
			street s;
			s.j1 = &junctions[j1];
			s.j2 = &junctions[j2];
			s.num = str;
			junctions[j1].qs.push(s);
			junctions[j2].qs.push(s);
		}
		if(cnt == 0) return 0;
		bool can = true;
		for(int i = 1; i <= maxi; i++){
			if(junctions[i].qs.size()%2){
				can = false;
				break;
			}
		}
		if(!can){
			cout << "Round trip does not exist." << endl;
			continue;
		}
		//cout << 1 << endl;
		bool state[2000] = {false};
		int remain = cnt;
		//int curJ = start;
		listNode lS;
		lS.j = &junctions[start];
		//cout << lS.next << endl;
		//cout << lS;
		//bool first = true;
		//lS.next = &lS;
		while(remain){
			listNode *iter = &lS;
			while(iter != NULL){
				junc *j = iter->j;
				if(!j->qs.empty()){
					while(!j->qs.empty() && state[j->qs.top().num]){
						j->qs.pop();
					}
				}
				if(!j->qs.empty()){
					break;
				}
				iter = iter->next;
			}
			//junc *dy = iter->j;
			//listNode *temp = iter->next;
			listNode ret;
			ret.j = iter->j;
			ret.next = iter->next;
			ret.streetNum = iter->streetNum;
			//cout << iter << endl;
			street temp = iter->j->qs.top();
			iter->j->qs.pop();
			state[temp.num] = true;
			//cout << temp.num << endl;
			remain --;
			iter->streetNum = temp.num;
			street curS = temp;
			while(1){
				listNode *Temp = new listNode();
				//cout << 1 << endl;

				if(iter->j == curS.j1) Temp->j = curS.j2;
				else Temp->j = curS.j1;
				iter->next = Temp;
				//cout << iter << endl;
				iter = iter->next;
				junc *j = Temp->j;
				if(!j->qs.empty()){
					while(!j->qs.empty() && state[j->qs.top().num]){
						j->qs.pop();
					}
				}
				if(j->qs.empty()){
					break;
				}
				curS = j->qs.top();
				j->qs.pop();
				state[curS.num] = true;
				//cout << curS.num << endl;
				iter->streetNum = curS.num;
				remain--;
				//cout << lS;
			}
			iter->j = ret.j;
			iter->next = ret.next;
			iter->streetNum = ret.streetNum;

		}
		listNode *it = &lS;
		//cout << *it ;
		while(it != NULL && it->next != NULL){
			cout << it->streetNum << " ";
			it = it->next;
		}
		cout << endl;
	}
	//cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
	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