-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6-3.cpp
More file actions
117 lines (108 loc) · 2.09 KB
/
Copy path6-3.cpp
File metadata and controls
117 lines (108 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include<iostream>
#include<cstring>
#include<queue>
#define INF 1e7
#define N 10
using namespace std;
struct node
{
int cl;
int id;
int x[N];
node() {}
node(int c,int i)
{
cl=c;
id=i;
memset(x,0,sizeof(x));
}
};
int m[N][N];
int bestx[N];
int bestl;
int n,M;
void init()
{
int i,j;
for(i=0; i<N; ++i)
for(j=0; j<N; ++j)
m[i][j]=INF;
memset(bestx,0,sizeof(bestx));
bestl=INF;
}
struct cmp
{
bool operator() (node n1,node n2)
{
return n1.cl>n2.cl;
}
};
void bfs()
{
priority_queue<node,vector<node>,cmp> q;
node temp(0,2);
int t;
for(int i=1; i<=n; ++i)
temp.x[i]=i;
q.push(temp);
node live;
while(!q.empty())
{
live=q.top();
q.pop();
t=live.id;
if(t==n)
{
if(m[live.x[t-1]][live.x[t]]!=INF&&m[live.x[t]][1]!=INF)
{
if(live.cl+m[live.x[t-1]][live.x[t]]+m[live.x[t]][1]<bestl)
{
bestl=live.cl+m[live.x[t-1]][live.x[t]]+m[live.x[t]][1];
for(int i=1; i<=n; ++i)
bestx[i]=live.x[i];
}
}
continue;
}
if(live.cl>=bestl)
continue;
for(int j=t; j<=n; ++j)
{
if(m[live.x[t-1]][live.x[j]]!=INF&&live.cl+m[live.x[t-1]][live.x[j]]<bestl)
{
temp=node(live.cl+m[live.x[t-1]][live.x[j]],t+1);
for(int k=1; k<=n; ++k)
temp.x[k]=live.x[k];
swap(temp.x[t],temp.x[j]);
q.push(temp);
}
}
}
}
void output()
{
cout<<"The shotest path length is:"<<bestl<<endl;
cout<<"The route is:";
for(int i=1; i<=n; ++i)
cout<<bestx[i]<<"-->";
cout<<bestx[1]<<endl;
}
int main()
{
init();
cin>>n;
cin>>M;
int i,a,b,c;
for(i=0; i<M; ++i)
{
cin>>a>>b>>c;
if(c<m[a][b])
{
m[a][b]=c;
m[b][a]=c;
}
}
bfs();
output();
return 0;
}