Travelling Salesman Problem

PAT-1150

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#include<sstream>
#include<set>
#include<map>
#include<cmath>
#include<vector>
#include<unordered_map>
using namespace std;
int n,m;
const int maxn=202;
const int maxm=40004;
const int INF=0X3F3F3F3F;
struct Edge{
int to;
int cost;
int next;
};
int city[maxn][maxn];
int main(){
cin>>n>>m;
memset(city,INF,sizeof(city));
for(int i=0;i<m;i++){
int city1,city2,dist;
cin>>city1>>city2>>dist;
city[city1][city2]=dist;
city[city2][city1]=dist;
city[city1][city1]=0;
city[city2][city2]=0;
}
int k;
cin>>k;
int mins=INF;
int minj=1;
for(int j=1;j<=k;j++){
int num;
cin>>num;
int num2=num;
int ans=0;
int start=0,endss;
map<int,int>ma;
int tot=0;//记录总共遍历了几个结点
int type=0;//0-simple,1-cycle,2-not
int path=0;
int pre=0;
while(num2--){
int c;
cin>>c;
if(ans==0){
start=c;
ma[c]++;
pre=start;
tot++;
}else{
if(ma[c]!=0){
if((num2!=0)||(num2==0&&start!=c)){
type=1;//非简单环
// cout<<num2<<" "<<c<<endl;
}
}else{
tot++;
ma[c]++;
}
path+=city[pre][c];
pre=c;
}
ans++;
if(ans==num)
endss=c;
}
if(path>=INF){
cout<<"Path "<<j<<": NA (Not a TS cycle)"<<endl;
}else{
if(tot!=n){
cout<<"Path "<<j<<": "<<path<<" (Not a TS cycle)"<<endl;
}else{
if(start!=endss){
cout<<"Path "<<j<<": "<<path<<" (Not a TS cycle)"<<endl;
continue;
}
if(type==1){
cout<<"Path "<<j<<": "<<path<<" (TS cycle)"<<endl;
}else{
cout<<"Path "<<j<<": "<<path<<" (TS simple cycle)"<<endl;
}
if(path<mins){
mins=path;
minj=j;
}
}
}
}
cout<<"Shortest Dist("<<minj<<") = "<<mins<<endl;
return 0;
}

PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断的更多相关文章

  1. PAT 甲级 1150 Travelling Salesman Problem

    https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...

  2. PAT A1150 Travelling Salesman Problem (25 分)——图的遍历

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  3. 1150 Travelling Salesman Problem(25 分)

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  4. 1150 Travelling Salesman Problem

    The "travelling salesman problem" asks the following question: "Given a list of citie ...

  5. hdu 5402 Travelling Salesman Problem(大模拟)

    Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...

  6. PAT_A1150#Travelling Salesman Problem

    Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...

  7. HDOJ 5402 Travelling Salesman Problem 模拟

    行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...

  8. HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)

    Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  9. 构造 - HDU 5402 Travelling Salesman Problem

    Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...

随机推荐

  1. Gym - 102062A、B、C、D、E、F、G、H

    比赛链接:https://vjudge.net/contest/409725#problem 题面点此处进入 Gym - 102062A 题意: 就是说比赛一共发a+b+c+d个牌子,现在不带上主人公 ...

  2. Is It A Tree? POJ - 1308

    题意: 题目给你一组单向边,当遇到输入0 0就证明这是一组边,当遇到-1 -1就要停止程序.让你判断这是不是一棵树 题解: 题目很简单,但是程序要考虑的很多 1.因为是一颗树,所以肯定不能出现环,这个 ...

  3. Linux CentOS7.x 升级内核的方法

    一.概述 在数据中心基础环境中,Linux系统使用很普遍,但是有时候会遇到应用程序需要运行在高版本的内核上或者有时候系统自身要求需要升级内核,我们要综合考虑升级内核的风险. 二.升级内核的方法 1.查 ...

  4. 2018大都会赛 A Fruit Ninja【随机数】

    题目链接:戳这里 题意:一个平面里有n个点,问存不存在一条直线上有m个点,满足m >= n*x. 解题思路:0<x<1,且x小数点后只有1位,也就是说10*m > n.假设存在 ...

  5. μC/OS-III---I笔记2---实钟节拍

    所谓时钟节拍,就是CPU以固定的频率产生中断,可以看做是系统的心跳.内核利用这个时钟节拍来管理各个任务的一些时间管理比如延时,定时,超时检测,时间轮片调度等.时钟节拍的频率一般10Hz--1000Hz ...

  6. h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated

    Reference 问题 ... h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype ...

  7. js code review

    js code review https://codereview.stackexchange.com/ refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只 ...

  8. Apple Watch Series 6 无法使用截屏问题和解决方案

    Apple Watch Series 6 无法使用截屏问题和解决方案 shit Apple,无法使用截屏, TMD 根本就不存在 相机胶卷 ! 不好使 解决方案 ??? https://support ...

  9. VSCode Plugin & Auto File Header Comments Generator

    VSCode Plugin & Auto File Header Comments Generator Xcode SwiftUI // // ContentView.swift // Mem ...

  10. short URL 短网址实现原理剖析

    short URL 短网址实现原理剖析 意义,简短便于分享,避免出现超长 URL 的字符长度限制问题 原理分析, 使用 HashMap 存储对应的映射关系 (长度不超过7的字符串,由大小写字母加数字共 ...