AcWing 1643. 旅行商问题
原题链接
简单
作者:
jjkstra
,
2020-07-12 17:04:58
,
所有人可见
,
阅读 1140
C++ 代码
#include <iostream>
#include <vector>
#include <set>
using namespace std;
const int N = 201;
const int INF = 1e6;
int g[N][N], x, dis = INF;
bool vis[N];
int main()
{
int n, m, k;
cin >> n >> m;
while (m--)
{
int u, v, w;
cin >> u >> v >> w;
g[u][v] = g[v][u] = w;
}
cin >> k;
for (int i = 1; i <= k; i++)
{
int c;
cin >> c;
vector<int> v(c);
set<int> s;
for (int i = 0; i < c; i++)
{
cin >> v[i];
s.insert(v[i]);
}
printf("Path %d: ", i);
int total = 0;
bool flag = true;
for (int i = 0; i < c - 1; i++)
{
int t = g[v[i]][v[i + 1]];
if (t)
total += t;
else
{
printf("NA (Not a TS cycle)\n");
flag = false;
break;
}
}
if (flag)
{
if (s.size() == n && v[0] == v[c - 1])
{
if (total < dis)
{
dis = total;
x = i;
}
if (c == n + 1)
printf("%d (TS simple cycle)\n", total);
else
printf("%d (TS cycle)\n", total);
}
else
printf("%d (Not a TS cycle)\n", total);
}
}
printf("Shortest Dist(%d) = %d", x, dis);
return 0;
}