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: 现有一 ... 
随机推荐
- poj 1410 (没做出来,记得闲着没事看看这道题)
			听说这道题是个大大的坑题 结果wa了十多发,,,,还是没找到原因 #include<cstdio> #include<cmath> #include<algorithm& ... 
- 【2020杭电多校】 Lead of Wisdom、The Oculus
			题目链接:Lead of Wisdom 题意:有n个物品,这些物品有k种类型.每种物品有对应的类型ti,其他值ai,bi,ci,di 你可以选择一些物品,但是这些物品要保证它们任意两者之间类型不能相同 ... 
- 洛谷 P6225 [eJOI2019]异或橙子 (树状数组)
			题意:有\(n\)个数,起始值均为\(0\),进行\(q\)次操作,每次输入三个数,如果第一个数为\(1\),则将第\(i\)个数修改为\(j\),如果为\(2\),则求区间\([l,r]\)内的所有 ... 
- 2017, X Samara Regional Intercollegiate Programming Contest   E. Bonuses and Teleports  (思维,模拟)
			题意:在\(x\)轴上有很多传送点和钻石,当位于传送点上时,可以传送到其他任意传送点(不记操作数),位于钻石上时可以吃掉它,每次可以移动一个单位,问最少多少次可以吃掉所有的钻石. 题解:对于某个位置上 ... 
- spring再学习之设计模式
			今天我们来聊一聊,spring中常用到的设计模式,在spring中常用的设计模式达到九种. 第一种:简单工厂 三种工厂模式:https://blog.csdn.net/xiaoddt/article/ ... 
- leetcode 周赛 205   1576-5508-5509-5510
			第四题比较难,看题解用并查集做比较简单,但是我觉得难度在想到用并查集,可能是最近做题少所以想不到吧. 1 替换所有的问号 class Solution { public: string modifyS ... 
- codeforces 920E(非原创)
			E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ... 
- CSS ::marker All In One
			CSS ::marker All In One CSS pseudo element / CSS 伪元素 /* user agent stylesheet */ ::marker { unicode- ... 
- Array.fill & array padding
			Array.fill & array padding arr.fill(value[, start[, end]]) https://developer.mozilla.org/en-US/d ... 
- HTTPS Proxy all in one
			HTTPS Proxy all in one HTTP Proxy Charles Proxy https://www.charlesproxy.com/ Proxy SwitchyOmega 轻松快 ... 
