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-08-31 04:30:58 on Problem 1237
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
	string s;
	while(cin >> s){
		int graph[26][26] = {0};
		int degree[26] = {0};
		int len = s.length();
		int total = len;
		graph[s[0]-'a'][s[len-1]-'a'] = len;
		graph[s[len-1]-'a'][s[0]-'a'] = len;
		degree[s[0]-'a']++;
		degree[s[len-1]-'a']++;
		while(cin >> s){
			if(s == "deadend") break;
			int len = s.length();
			graph[s[0]-'a'][s[len-1]-'a'] = len;
			graph[s[len-1]-'a'][s[0]-'a'] = len;
			degree[s[0]-'a']++;
			degree[s[len-1]-'a']++;
			total += len;
		}
		int start = -1, end = -1;
		for(int i = 0; i < 26; i++){
			if(degree[i]%2 == 1 && start == -1) start = i;
			else if(degree[i]%2 == 1) end = i;
		}
		if(start == -1){
			cout << total << endl;
			continue;
		}
		int dist[26];
		for(int i = 0; i < 26; i++) dist[i] = 2147483647;
		dist[start] = 0;
		bool used[26] = {0};
		while(1){
			int mn = 2147483647, arg = -1;
			for(int i = 0; i < 26; i++){
				if(!used[i] && (dist[i] < mn)) {
					mn = dist[i];
					arg = i;
				}
			}
			used[arg] = 1;
			if(arg == end) break;
			for(int i = 0; i < 26; i++){
				if(used[i] || graph[arg][i] == 0) continue;
				int newDist = graph[arg][i] + dist[arg];
				if(newDist < dist[i]) dist[i] = newDist;
			}
		}
		cout << total + dist[end] << 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