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: 现有一 ...
随机推荐
- C. Table Decorations
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Vitya and Strange Lesson CodeForces - 842D 字典树+交换节点
题意: Today at the lesson Vitya learned a very interesting function - mex. Mex of a sequence of number ...
- MHA 高可用介绍
目录 MHA 介绍 MHA 简介(Master High Availability) MHA 工作原理(转载) MHA 架构 MHA 工具 Manager 节点 Node 节点 MHA 优点 MHA ...
- LINUX - 通信
为什么三次握手: 让服务端和客户端都知道,自己的收信能力和发信能力没有问题: 第一次:客户端发给服务端--服务端知道了,自己的收信能力和客户端的发信能力没有问题: 第二次:服务端回复客户端--客户端知 ...
- 【非原创】sg函数模板
学习博客:戳这里 解题模型: 1.把原游戏分解成多个独立的子游戏,则原游戏的SG函数值是它的所有子游戏的SG函数值的异或. 即sg(G)=sg(G1)^sg(G2)^...^sg(Gn) ...
- mysql(一)--mysql架构和执行流程
1. 一条查询 SQL 语句是如何执行的? 我们的程序或者工具要操作数据库,第一步要做什么事情? 跟数据库建立连接. 1.1. 通信协议 首先,MySQL 必须要运行一个服务,监听默认的 3306 ...
- JWT实现登录认证实例
JWT全称JSON Web Token,是一个紧凑的,自包含的,安全的信息交换协议.JWT有很多方面的应用,例如权限认证,信息交换等.本文将简单介绍JWT登录权限认证的一个实例操作. JWT组成 JW ...
- JVM系列之一 JVM的基础概念与内存区域
前言 作为一名 Java 语言的使用者,学习 JVM 有助于解决程序运行过程中出现的问题.写出性能更高的代码. 可以说:学好 JVM 是成为中高级 Java 工程师的必经之路. 有感于从未整理归纳 J ...
- WoT
WoT IoT / AIoT Web of Things (WoT) Architecture W3C Recommendation 9 April 2020 https://www.w3.org/T ...
- cookie all in one
cookie all in one credentials: "include" https://developers.google.com/web/updates/2015/03 ...