湘潭邀请赛 Hamiltonian Path

时间:2023-03-08 16:57:43

湘潭邀请赛的C题,哈密顿路径,边为有向且给定的所有边起点小于终点,怎么感觉是脑筋急转弯?

以后一定要牢记思维活跃一点,把复杂的事情尽量简单化而不是简单的事情复杂化。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
const int N = 100008, INF = 0x3F3F3F3F;
#define MS(a, num) memset(a, num, sizeof(a))
#define PB(A) push_back(A)
#define FOR(i, n) for(int i = 0; i < n; i++)
int dis[N];
int main(){
int n, m;
while(~scanf("%d %d", &n, &m)){
memset(dis, 0x3F, sizeof(dis));
while(m--){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if(a + 1 == b){
dis[a] = min(dis[a], c);
}
}
int ans = 0;
for(int i = 1; i < n; i++){
if(dis[i] >= INF){
ans = -1;
break;
}
ans += dis[i];
}
cout<<ans<<'\n';
}
return 0;
}