当前位置:网站首页>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;
}
边栏推荐
- K high frequency elements before sorting
- Is pioneer futures safe? What are the procedures for opening futures accounts? How to reduce the futures commission?
- [extensive reading of papers] attributes guided facial image completion
- Programming exercises: whole point and circle (solution ideas and code implementation)
- HD mechanical principle · classic dynamic drawing of mechanical design
- V3 03_ Getting started
- 1136: password translation
- 2021-08-05 leetcode notes
- Examples of bubble sorting and matrix element screening in MATLAB
- Binary rotation array (1)
猜你喜欢

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

Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~
![【BUUCTF】[GXYCTF2019]Ping Ping Ping1](/img/dc/4d87dfb0c2fa9cd75b54e092fd3971.jpg)
【BUUCTF】[GXYCTF2019]Ping Ping Ping1

Laravel upload error

CCF adjacent number pairs (Full Score code + problem solving ideas + skill summary) 201409-1

How to use Alibaba Vector Icon

The first dark spring cup dnuictf

CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
![[buuctf] [actf2020 freshman competition]include](/img/42/50439290177fdea5f431e315cac1a1.jpg)
[buuctf] [actf2020 freshman competition]include

Clear the route cache in Vue
随机推荐
Shift operator (detailed)
Solve the problem that codeblocks20.03 on win11 cannot run for the first time
Non decreasing column
For loop and promise to solve the problem of concurrent callback
这种零件该怎么编程加工?
Uniapp upload image method
[untitled]
PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
The first dark spring cup dnuictf
Analysis on the problems of irregular step hole on horizontal machining center
Invalid argument during startup: Failed to open the . conf file: redis-window
立式加工中心的数控加工对刀具使用基本要求
catkin_ Make reports an error, transfers the location of the workspace, and uses other people's workspace files to cause compilation errors
An error is reported when installing dataspherestudio doc: invalid default value for 'update_ time‘
Three ways and differences of defining functions in JS
[extensive reading of papers] multimodal joint attribute prediction and value extraction for e-commerce product
1137: encrypted medical record
2021-05-12
Judgment of deep learning experiment results
Double pointer letter matching