这个题一开始直接是懵的,冷静下来就是看是不是每一次都是选了最短路径。
我看了其他人都是在选的时候来判断,我自己是DFS一次后。再用输入来记录每一次起点到下一个节点时的最大距离,如果起点到后面的节点小于最大值,那么就代表之前选取是错误的,No!
还有一点记得,如果是vector的clear函数是直接清零,v(n),直接变为长度为0
#include <bits/stdc++.h>
using namespace std;
const int INF = 999999999;
int G[1010][1010], start, n;
vector<int> dis, temp; //jide resize (n + 1)
vector<vector<int>> D, v;
bool vis[1010];
void Dijkstra(int s) {
fill (dis.begin(), dis.end(), INF);
fill (vis, vis + 1010, false);
dis[s] = 0;
for (int i = 1; i <= n; i++) {
int u = -1, MIN = INF;
for (int j = 1; j <= n; j++) {
if (vis[j] == false && dis[j] < MIN) {
u = j;
MIN = dis[j];
}
}
if (u == -1) return;
vis[u] = true;
for (int v = 1; v <= n; v++) {
if (vis[v] == false && G[u][v] != INF) {
if (dis[u] + G[u][v] < dis[v])
dis[v] = dis[u] + G[u][v];
}
}
}
}
int main() {
fill (G[0], G[0] + 1010 * 1010, INF);
int e, a, b, cost, m;
scanf ("%d %d", &n, &e);
dis.resize(n + 1);
D.resize(n + 1);
v.resize(n + 1);
temp.resize(n);
for (int i = 0; i < e; i++) {
scanf ("%d %d %d", &a, &b, &cost);
G[a][b] = G[b][a] = cost;
}
// cout << D[1][4] << endl;
scanf ("%d", &m);
for (int i = 0; i < m; i++) {
int flag = 0;
for (int j = 0; j < n; j++) scanf ("%d", &temp[j]);
start = temp[0];
Dijkstra(start);
int max = 0;
for (int j = 1; j < temp.size(); j++) {
if (dis[temp[j]] >= max) max = dis[temp[j]];
else {
flag = 1;
break;
}
}
if (flag == 0) printf ("Yes\n");
else printf ("No\n");
}
}