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: 现有一 ...
随机推荐
- python实现通过指定浏览器免费观看vip视频
程序是先通过一个解析视频的网站,然后我们提取其接口,然后实现观看vip视频的目的 所以说免费观看视频python程序很容易,但是下载视频就有些许麻烦了,下载视频请见我另一篇博客:python+fidd ...
- Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2) A. Prison Break
题意:有一张\(n\)x\(m\)的图,图中每个点都关押着罪犯,在坐标\((r,c)\)处有一个出口,每名罪犯每秒可以可以像上下最有移动一个单位或者不动,问所有罪犯能够逃离监狱的最少时间. 题解:直接 ...
- CodeForces - 220B 离散化+莫队算法
莫队算法链接:传送门 题意: 有n个数,m个区间.问区间内有多少个x,x满足x的个数等于x的值的个数(如果x是3,区间内要存在3个3). 题解: 因为a[i]太大,所以要离散化一下,但是不能用map容 ...
- Codeforces Round #479 (Div. 3) E. Cyclic Components (思维,DFS)
题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环. 题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后 ...
- 2019-2020 ACM-ICPC Brazil Subregional Programming Contest Problem M Maratona Brasileira de Popcorn (二分)
题意:有\(n\)袋爆米花,某个队伍有\(c\)个队员,每个队员每秒做多可以吃\(t\)粒爆米花,但一袋爆米花只能由一个队员吃完,并且一个队员只能吃连续的一袋或几袋,不能隔着吃某一袋,求将所有爆米花吃 ...
- IGS OPC UA 配置
igs项目-右键属性-选择OPC UA,如图配置 ,其他默认 如果打开的是IGS-administration,在右下角会有通知栏图标,右键图标选择 OPC UA 配置 添加服务器节点,网络适配器选择 ...
- 如何在 Apple Watch S6上离线播放音乐
如何在 Apple Watch S6上离线播放音乐 Apple Watch 离线播放音乐 营销策略,捆绑销售 Apple Watch + AirPods + Apple Music Apple Wat ...
- Linux 学习笔记分享: Linux 用户组的权限: drwx------ 700权限(d 目录 ,r=4,w=2,x=1:rwx=7;---=0;---=0)
Linux 用户组的权限: drwx------ 700权限(d 目录 ,r=4,w=2,x=1:rwx=7;---=0;---=0) 1 1 Linux 用户组的权限: drwx------ 700 ...
- web components & publish custom element & npm
web components & publish custom element & npm https://www.webcomponents.org/publish Polymer ...
- better-scroll使用参考
************better-scroll是基于父元素固定高度,溢出才滚动的,所以父元素务必定高,否则无法滚动***************************************** ...