-
Notifications
You must be signed in to change notification settings - Fork 0
/
task40-comehome.c
53 lines (44 loc) · 936 Bytes
/
task40-comehome.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
ID: zltsang1
LANG: C
TASK: comehome
*/
#include <stdio.h>
#define xn 26
int adj[xn*2+2][xn*2+2];
int c2n(char v){
if(v>='a'&&v<='z')return v-'a';
if(v>='A'&&v<='Z')return v-'A'+xn;
}
main () {
FILE *fin = fopen ("comehome.in", "r");
FILE *fout = fopen ("comehome.out", "w");
int an,ret;
int i,j,k,t;
char a,b;
fscanf (fin, "%d\n", &an);
for(i=0;i<an;i++){
fscanf (fin, "%c %c %d\n", &a,&b,&t);
if(!adj[c2n(a)][c2n(b)]||adj[c2n(a)][c2n(b)]>t){
adj[c2n(a)][c2n(b)]=t;
adj[c2n(b)][c2n(a)]=t;
}
}
for(k=0;k<xn*2-1;k++){
for(i=0;i<xn*2;i++){
for(j=0;j<xn*2;j++){
if(i!=j&&adj[i][k]&&adj[j][k]&&(!adj[i][j]||adj[i][j]>adj[i][k]+adj[j][k]))
adj[i][j]=adj[i][k]+adj[j][k];
}
}
}
t=0;ret=xn-1;
for(i=xn;i<xn*2-1;i++){
if(adj[i][xn*2-1]&&(!t||t>adj[i][xn*2-1])){
ret=i-xn;
t=adj[i][xn*2-1];
}
}
fprintf (fout, "%c %d\n",(char)(ret+'A'),t);
exit (0);
}