题意:见上图。(用翻译插件翻译了一下)是一个多起点单终点的问题。
算法:digkstra
问题:以为是双向,结果wa了;改成单向过了,虽然不知道为什么是单向。
思路: 设置一个源起点连接数个起点,设置长度为0,转化为普通问题求解。
代码:ac
#include<bits/stdc++.h>
using namespace std;
#define inf 0x7fffffff
int Map[1010][1010];
int vis[1010];
int Dist[1010];
int n, m;
int s, e;
int p, q, t;
int w;
int i, j;
int Min;
int nex;
int main()
{
while (scanf("%d%d%d", &n, &m, &e)!=EOF) {
for (i = 0; i <= n; i++) {
Dist[i] = inf;
vis[i] = 0;
for (j = 1; j <= n; j++)
Map[i][j] = inf;
}
while (m--) {
scanf("%d%d%d", &p, &q, &t);
Map[p][q] = min(Map[p][q], t);
}
s = 0;
scanf("%d", &w);
while (w--) {
int temp;
scanf("%d", &temp);
Map[s][temp]= 0;
}
vis[s] = 1;
Dist[s] = 0;
while (s != e) {
Min = inf;
for (i = 1; i <= n; i++) {
if (Map[s][i] != inf)
Dist[i] = min(Dist[i], Dist[s] + Map[s][i]);
if (!vis[i] && Dist[i] < Min) {
nex = i;
Min = Dist[nex];
}
}
if (Min == inf)break;
s = nex;
vis[nex] = 1;
}
if (Dist[e] == inf)printf("-1\n");
else printf("%d\n", Dist[e]);
}
return 0;
}