| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
裸的欧啦路判断+迪杰特斯拉#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator