题干:1150 Travelling Salesman Problem (25 分)
题解:第3个的测试点答案错误,第4个测试点段错误,最后拿了19分,我自己去排查了一下,加了一些防护的措施,但是发现后面还是没有什么用,但是我已经很满意了,我初步估计我考试的话能够拿70分就比较好了,就是说4道题都做出来,但是都有一点错误,因为后面的错误排查确实是相当复杂的,还是缺乏这种编程的经验,尤其是在这么短的环境里面,但是有的时候和满分的答案相比,就是之前的设计思路有点不同导致了最后的一个差异吧,还是可以接受这个结果的。。。。其实这个也看一个在考场上面的运气了
// A1150.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <bits/stdc++.h>
using namespace std;
#define max 100
struct elem {
int id;
int dist;
};
vector<elem> ans;
int main()
{
#ifndef ONLINE_JUDGE
#pragma warning(disable:4996)
freopen("in.txt", "r", stdin);
#endif // !ONLINE_JUDGE
int n, m,c1,c2,d,k;
cin >> n >> m;
vector<vector<int>> gn(n + 1, vector<int>(n + 1, max));
for (int i = 0; i < m; i++) {
cin >> c1 >> c2 >> d;
gn[c1][c2] = d;
gn[c2][c1] = d;
}
cin >> k;
int n1;
for (int i = 0; i < k; i++) {
cin >> n1;
vector<int> an(n1);
for (int j = 0; j < n1; j++) {
cin >> an[j];
}//input
bool flag=true;
int sum = 0;
int dt=max;
int simple = true;
vector<int> bn(n+1, 0);//按照序号来的
bn[an[0]] = true;
if (n1 != 1) {
for (int j = 1; j < n1; j++) {
dt = gn[an[j - 1]][an[j]];
if (dt == max) {
flag = false;
break;
}
else {
bn[an[j]]++;
sum += dt;
}
}
}
else {
flag = false;
}
if (an[0] != an[n1 - 1]) {
flag = false;
}
for (int j = 1; j <= n; j++) {//n是总的序号
if (bn[j] == 0) {
flag = false;
break;
}
if (bn[j] > 1) {
if (j != an[0]) {
simple = false;
}
}
if (bn[j] > 2) {
simple = false;
}
}
if (flag==false&&dt==max) {
printf("Path %d: NA (Not a TS cycle)\n", i + 1);
}
if (flag == false && dt != max) {
printf("Path %d: %d (Not a TS cycle)\n", i + 1,sum);
}
if (flag == true && simple == false) {
printf("Path %d: %d (TS cycle)\n", i + 1, sum);
}
if (flag == true && simple == true) {
printf("Path %d: %d (TS simple cycle)\n", i + 1, sum);
}
if (flag == true) {
ans.push_back({ i + 1,sum });
}
}
int f=101;
int p = -1;
for (int i = 0; i < ans.size(); i++) {
if (ans[i].dist < f) {
f = ans[i].dist;
p = i;
}
}
printf("Shortest Dist(%d) = %d", ans[p].id, ans[p].dist);
return 0;
}
别人的满分代码
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int e[300][300], n, m, k, ans = 99999999, ansid;
vector<int> v;
void check(int index) {
int sum = 0, cnt, flag = 1;
scanf("%d", &cnt);
set<int> s;
vector<int> v(cnt);
for (int i = 0; i < cnt; i++) {
scanf("%d", &v[i]);
s.insert(v[i]);
}
for (int i = 0; i < cnt - 1; i++) {
if(e[v[i]][v[i+1]] == 0) flag = 0;
sum += e[v[i]][v[i+1]];
}
if (flag == 0) {
printf("Path %d: NA (Not a TS cycle)\n", index);
} else if(v[0] != v[cnt-1] || s.size() != n) {
printf("Path %d: %d (Not a TS cycle)\n", index, sum);
} else if(cnt != n + 1) {
printf("Path %d: %d (TS cycle)\n", index, sum);
if (sum < ans) {
ans = sum;
ansid = index;
}
} else {
printf("Path %d: %d (TS simple cycle)\n", index, sum);
if (sum < ans) {
ans = sum;
ansid = index;
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int t1, t2, t;
scanf("%d%d%d", &t1, &t2, &t);
e[t1][t2] = e[t2][t1] = t;
}
scanf("%d", &k);
for (int i = 1; i <= k; i++) check(i);
printf("Shortest Dist(%d) = %d\n", ansid, ans);
return 0;
}