标签:mes htm 最短路径 book turn limit problem 杭电 pre
Time Limit: 1000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 31420????Accepted Submission(s): 10804
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;
}
标签:mes htm 最短路径 book turn limit problem 杭电 pre
原文地址:https://www.cnblogs.com/RioTian/p/12777514.html