一个人的旅行
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31420 Accepted Submission(s): 10804
Problem Description
Input
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。
Output
Sample Input
Sample Output
AC代码:
#include<bits/stdc++.h>
#define N 1005
#define inf 9999999
using namespace std;
int e[N][N], dis[N],book[N];
void dijkstra(int n) {
int i, j, k, Min;
memset(book, 0, sizeof(book));
//初始化某点的最短路径
for (i = 0; i <= n; ++i)dis[i] = e[0][i];
book[0] = 1;
for (k = 1; k <= n; ++k) {
Min = inf;
for (i = 1; i <= n; ++i)
if (!book[i] && dis[i] < Min)Min = dis[i], j = i;
book[j] = 1;
for (i = 1; i <= n; ++i)
if (!book[i] && dis[i] > e[j][i] + dis[j])
dis[i] = e[j][i] + dis[j];
}
}
int main() {
int t, s, d, Min, u, v, w, n;
while (cin >> t >> s >> d) {
memset(e, inf, sizeof(e));
for (int i = 0; i < N; ++i)e[i][i] = 0;
n = 0;
//输入城市间路径与时间
while (t--) {
cin >> u >> v >> w;
//因为有些路径是给了多次,取最小的
if (e[u][v] > w)e[u][v] = e[v][u] = w;
n = max(n, max(u, v));
}
//相邻城市距离为0
while (s--) {
cin >> v;
e[0][v]= e[v][0] = 0;
}
dijkstra(n);
Min = inf;
while (d--) {
cin >> v;
Min = min(Min, dis[v]);
}
cout << Min << endl;
}
return 0;
}