AcWing 1375. 奶牛回家
原题链接
简单
作者:
20岁带专生陈刀仔
,
2021-04-24 20:46:41
,
所有人可见
,
阅读 389
1~26为a~z–空牧场 27~51为A~Y–有牛牧场 52为Z–牛棚
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#define N 53
#define INF 0x3f3f3f3f
int P;
int g[N][N];
int dist[N];
bool st[N];
int min(int a, int b){ return a < b ? a : b; }
void dijkstra(int x){
memset(dist, 0x3f, sizeof dist);
dist[x] = 0;
for(int i = 1; i <= 52; i++){
int t = -1;
for(int j = 1; j <= 52; j++)
if(!st[j] && (t == -1 || dist[t] > dist[j]))
t = j;
st[t] = true;
for(int j = 1; j <= 52; j++)
dist[j] = min(dist[j], dist[t] + g[t][j]);
}
}
int main(){
scanf("%d", &P);
memset(g, 0x3f, sizeof g);
while(P--){
char A, B;
int a, b, w;
scanf(" %c %c%d", &A, &B, &w);
if(A - 'a' >= 0 && A - 'a' <= 25) a = A - 'a' + 1;
if(A - 'A' >= 0 && A - 'A' <= 25) a = A - 'A' + 27;
if(B - 'a' >= 0 && B - 'a' <= 25) b = B - 'a' + 1;
if(B - 'A' >= 0 && B - 'A' <= 25) b = B - 'A' + 27;
g[a][b] = g[b][a] = min(g[a][b], w);
}
dijkstra(52);
int min = INF, t;
for(int i = 27; i <= 51; i++)
if(dist[i] < min){
min = dist[i];
t = i;
}
char T = t - 26 + 64;
printf("%c %d", T, min);
return 0;
}