PAT-1150(Travelling Salesman Problem)旅行商问题简化+模拟图+简单回路判断
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)旅行商问题简化+模拟图+简单回路判断的更多相关文章
- PAT 甲级 1150 Travelling Salesman Problem
https://pintia.cn/problem-sets/994805342720868352/problems/1038430013544464384 The "travelling ...
- PAT A1150 Travelling Salesman Problem (25 分)——图的遍历
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- 1150 Travelling Salesman Problem(25 分)
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- 1150 Travelling Salesman Problem
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- hdu 5402 Travelling Salesman Problem(大模拟)
Problem Description Teacher Mai ,) to the bottom right corner (n,m). He can choose one direction and ...
- PAT_A1150#Travelling Salesman Problem
Source: PAT A1150 Travelling Salesman Problem (25 分) Description: The "travelling salesman prob ...
- HDOJ 5402 Travelling Salesman Problem 模拟
行数或列数为奇数就能够所有走完. 行数和列数都是偶数,能够选择空出一个(x+y)为奇数的点. 假设要空出一个(x+y)为偶数的点,则必须空出其它(x+y)为奇数的点 Travelling Salesm ...
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 构造 - HDU 5402 Travelling Salesman Problem
Travelling Salesman Problem Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5402 Mean: 现有一 ...
随机推荐
- B. Queue
During the lunch break all n Berland State University students lined up in the food court. However, ...
- hdu2852 KiKi's K-Number
Problem Description For the k-th number, we all should be very familiar with it. Of course,to kiki i ...
- c语言实现链表增、删、改、查及文件读写 && 链表实现程序
一.链表实现增删改查 1.链表定义 1 #include<stdio.h> 2 #include<string.h> 3 #include<windows.h> 4 ...
- c#中几种常见的数据结构
数组(Array): 1.数组存储在连续的内存上 2.数组的元素类型必须相同 3.数组可以直接通过下标访问 4.查找与修改元素的速度非常快 5.必须在声明时指定长度 动态数组(ArrayLis ...
- Selenium+Python之下拉菜单的定位
1.通过selenium.webdriver.support.ui的Select进行定位 下拉菜单如下图: 定位代码(选择Male): from selenium.webdriver.support. ...
- Dapr微服务应用开发系列2:Hello World与SDK初接触
题记:上篇介绍了Dapr的环境配置,这次我们来动手尝试一下Dapr应用的开发 Hello World Dapr应用的Hello World其实和其他的Hello World一样简单: 首先用你喜欢的语 ...
- Linux 驱动框架---linux 驱动
总述 Linux 系统下的驱动最后都是以如下这个结构体呈现在系统中的,注意其中的dev_pm_ops是内核新增的内容来准备替换platform_driver中的电源管理相关的内容.这里内容是先进行总体 ...
- 惠普机械键盘 K10GL 使用评测
惠普机械键盘 GK100 使用评测 手感太差,不是 RGB 背光 惠普(HP) K10GL 机械键盘 有线 LED背光机械键盘 87键 混光青轴 refs https://item.jd.com/10 ...
- ESLint All In One
ESLint All In One ESLint $ yarn add -D eslint .eslintrc.{js,yml,json} 优先级 .eslintrc .eslintrc.js .es ...
- CSS animation & CSS animation 101
CSS animation 101 如何为 Web 添加动画效果. https://github.com/cssanimation/css-animation-101 https://github.c ...