-
Notifications
You must be signed in to change notification settings - Fork 2
/
POJ_1135_Domino Effect_Dijkstra.cpp
118 lines (114 loc) · 2.17 KB
/
POJ_1135_Domino Effect_Dijkstra.cpp
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
118
#include <iostream>
#include <limits>
#include <iomanip>
using namespace std;
int extract_min(int *d,bool *visited,int n)
{
int mini=numeric_limits<int>::max();
int index=-1;
for(int i=1;i<n;++i){
if(!visited[i] && d[i]<mini){
mini=d[i];
index=i;
}
}
return index;
}
void relax(int **mat,int *d,bool *visited,int index,int n)//邻接结点松弛操作
{
for(int i=1;i<n;++i){
if(i!=index && !visited[i] && mat[index][i]!=numeric_limits<int>::max()){
if(d[index]+mat[index][i]<d[i]){
d[i]=d[index]+mat[index][i];
}
}
}
}
void print(int **mat,int* d,int n,int m,int num)
{
double maxi=0;
int p1=1,p2=1;
double tmp;
for(int i=0;i<n;++i){
for(int j=1;j<n;++j){
if(i!=j && mat[i][j]!=numeric_limits<int>::max()){
tmp=1.0*(d[i]+d[j]+mat[i][j])/2;
if(tmp>maxi){
maxi=tmp;
p1=i+1,p2=j+1;
}
}
}
}
cout<<"System #"<<num<<endl;
cout << std::fixed<< std::setprecision(1);
cout<<"The last domino falls after "<<maxi<<" seconds, ";
if(d[p1-1]==maxi){
cout<<"at key domino "<<p1<<"."<<endl;
}else if(d[p2-1]==maxi){
cout<<"at key domino "<<p2<<"."<<endl;
}else{
cout<<"between key dominoes ";
if(p1>p2){
int tmp=p1;
p1=p2;
p2=tmp;
}
cout<<p1<<" and "<<p2<<"."<<endl;
}
cout<<endl;
}
void dijkstra(int **mat,int *d,bool *visited,int n)//单源最短路径优先
{
int left=n;
int index;
while(--left){
index=extract_min(d,visited,n);
relax(mat,d,visited,index,n);
visited[index]=true;
}
}
int main()
{
int n,m;
int a,b,l;
int **mat;//邻接矩阵
int *d;
bool *visited;
int num=0;
while(cin>>n>>m){
if(n==0 && m==0){
break;
}
mat=new int *[n];
for(int i=0;i<n;++i){
mat[i]=new int [n];
for(int j=0;j<n;++j){
mat[i][j]=numeric_limits<int>::max();
}
mat[i][i]=0;
}
for(int i=0;i<m;++i){
cin>>a>>b>>l;
mat[a-1][b-1]=l;
mat[b-1][a-1]=l;
}
d=new int[n];
visited=new bool[n];
for(int i=0;i<n;++i){
d[i]=mat[0][i];
visited[i]=false;
}
visited[0]=true;//第一个结点是起始结点
dijkstra(mat,d,visited,n);
++num;
print(mat,d,n,m,num);
for(int i=0;i<n;++i){
delete []mat[i];
}
delete []mat;
delete []d;
delete []visited;
}
return 0;
}