problem
solution
codes
//标程Dijkstra
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
//T
int n = 60, m, vis[1010];
//Graph
struct Edge{
int v, w;
Edge(int v, int w):v(v),w(w){}
};
vector<Edge>G[70];
//Dijkstra
int dis[70], book[70], s = (int)'Z'-'A';
void Dijkstra(){
for(int i = 0; i < n; i++)dis[i] = 0xffffff;
for(int i = 0; i < G[s].size(); i++)//bugs数据可能有覆盖
dis[G[s][i].v] = min(dis[G[s][i].v],G[s][i].w);
dis[s] = 0; book[s] = 1;
for(int i = 0; i < n; i++){
int v, w=0xffffff;
for(int j = 0; j < n; j++)
if(!book[j] && dis[j]<w)
w = dis[v=j];
book[v] = 1;
for(int j = 0; j < G[v].size(); j++)
dis[G[v][j].v] = min(dis[G[v][j].v], w+G[v][j].w);
}
}
int main(){
cin>>m;
for(int i = 1; i <= m; i++){
char a, b; int w; cin>>a>>b>>w;
int x = a-'A', y = b-'A';
if(x>=0 && x<25)vis[x] = 1;
if(y>=0 && y<25)vis[y] = 1;
G[x].push_back(Edge(y,w));
G[y].push_back(Edge(x,w));
}
Dijkstra();
int v, w=0xffffff;
for(int i = 0; i < n; i++)
if(vis[i] && dis[i]<w)
w = dis[v=i];
cout<<(char)(v+'A')<<" "<<w<<"\n";
return 0;
}
//数据太小, Floyd一番水
#include<iostream>
#include<algorithm>
using namespace std;
int n = 60, m;
int e[1010][1010], vis[1010];
int main(){
cin>>m;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
e[i][j] = i==j ? 0 : 0xffffff;
for(int i = 0; i < m; i++){
char a, b; int w; cin>>a>>b>>w;
int x = a-'A', y = b-'A';
if(x>=0 && x<25)vis[x] = 1;
if(y>=0 && y<25)vis[y] = 1;
//bugs数据可能有覆盖
e[x][y] = min(e[x][y], w);
e[y][x] = min(e[y][x], w);
}
for(int k = 0; k < n; k++)
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
e[i][j] = min(e[i][j], e[i][k]+e[j][k]);
int ans2 = 0xffffff, ans1;
for(int i = 0; i < n; i++)if(vis[i] && e[i][(int)'Z'-'A']<ans2){
ans2 = e[i][(int)'Z'-'A'];
ans1 = i;
}
cout<<(char)(ans1+'A')<<" "<<ans2;
return 0;
}