当前位置:网站首页>1150 traveling salesman problem (25 points)
1150 traveling salesman problem (25 points)
2022-06-30 14:55:00 【Xue Dongjing】
maxmin
1150 Travelling Salesman Problem (25 branch )
The question
Give an undirected graph , and k Paths , Determine which paths are simple loops , Not a simple loop , Non loop , Disconnected path . And find the least expensive way in the loop .
1. Simple loop : Except for the starting point, all other points pass through once , And start from the starting point and finally return to the starting point .
2. Not a simple loop : From the beginning , Finally back to the starting point , Each point passes at least once .
3. Non loop : Not passing through all points or starting and ending points are different .( It is not without rings )
4. Unconnected : The path is disconnected .
Ideas
Traverse every path , When there is an unconnected path, it is directly 4, A little bit has not passed or the starting point and ending point are different 3, be left over , There are only nodes in the path +1 The path of nodes of is 1, The last remaining is 2.
Find the minimum cost in the loop .
Note that the initialization is a little larger when finding the minimum cost (>1e5), The last test point card .
Code
#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
int mp[507][507];
int main()
{
int n,m,a,b,c,k,x,y[507],last,vis[507],sum,flog=0,xmin=100000,xxx;//xmin initial value >=1e5
vector<int>s;
memset(mp,0,sizeof(mp));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&c);
mp[a][b]=c;
mp[b][a]=c;
}
scanf("%d",&k);
for(int i=0;i<k;i++){
scanf("%d",&x);
flog=0;
sum=0;
memset(vis,0,sizeof(vis));
for(int j=0;j<x;j++){
scanf("%d",&y[j]);
if(j!=0){
// printf("#\n");
if(flog!=1&&mp[last][y[j]]!=0){
sum+=mp[last][y[j]];
vis[y[j]]=1;
last=y[j];
}else{
flog=1;
}
}else{
last=y[j];
vis[y[j]]=1;
}
}
if(flog==1){
printf("Path %d: NA (Not a TS cycle)\n",i+1);
}else{
for(int j=1;j<=n;j++){
if(vis[j]!=1){
flog=1;
break;
}
}
if(flog==1||y[0]!=y[x-1]){
printf("Path %d: %d (Not a TS cycle)\n",i+1,sum);
}else if(x==n+1){
printf("Path %d: %d (TS simple cycle)\n",i+1,sum);
if(xmin>sum){
xmin=sum;
xxx=i+1;
}
}else{
printf("Path %d: %d (TS cycle)\n",i+1,sum);
if(xmin>sum){
xmin=sum;
xxx=i+1;
}
}
}
}
printf("Shortest Dist(%d) = %d\n",xxx,xmin);
return 0;
}
边栏推荐
- Computer screenshot how to cut the mouse in
- [extensive reading of papers] analyzing connections between user attributes, images, and text
- PS dynamic drawing
- 1133: output family and friends string
- PHP generate images into Base64
- Maximum area of islands searched
- Examples of bubble sorting and matrix element screening in MATLAB
- catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors
- Win10 backup backup shows that creating a shared protection point on the shadow failed
- How does hbuilder display in columns?
猜你喜欢

@PathVariable

How to use Alibaba Vector Icon

PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available

How does hbuilder display in columns?

Sum of CCF digits (full mark code + problem solving idea) 201512-1

Clear the route cache in Vue
![[buuctf] [actf2020 freshman competition]include](/img/42/50439290177fdea5f431e315cac1a1.jpg)
[buuctf] [actf2020 freshman competition]include

CCF window (Full Score code + problem solving idea) March 2, 2014

PS dynamic drawing

Lihongyi machine learning 2020 homework summary
随机推荐
文本匹配——【NAACL 2021】AugSBERT
Finding the median of two arrays by dichotomy
One dimensional and two dimensional array addresses
分布式--OpenResty+lua+Redis
Repair of incorrect deletion of win10 boot entry
Learn about data kinship JSON format design from sqlflow JSON format
Sorting by character frequency
catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors
1 figure to explain the difference and connection between nodejs and JS
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
[buuctf] [actf2020 freshman competition]include
Average and maximum values of MATLAB matrix
[extensive reading of papers] attributes guided facial image completion
[untitled]
Laravel8 custom log directory, rename
1130: find the first character that appears only once
2021-07-15Caused by: org. quartz. ObjectAlreadyExistsException: Unable to store Job : ‘DEFAULT. TASK_ 1‘
Solve the problem that codeblocks20.03 on win11 cannot run for the first time
CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1
V3 03_ Getting started