-
Notifications
You must be signed in to change notification settings - Fork 0
/
1072.cpp
177 lines (157 loc) · 2.96 KB
/
1072.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
最短路径算法的应用
*/
#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <stdio.h>
using namespace std;
vector< set<int> > graph; /* 用类似临接矩阵的方式记录节点间的关系*/
vector< vector<float> > records;
vector<float> sPath;
const float MAXLEN = 0xffffff;
const int MAXNUM = 1020;
float weight[MAXNUM][MAXNUM];
int N, M, K;
float Ds;
bool visited[1020];
typedef struct elem
{
int index;
float minlen;
float maxlen;
float total;
}elem;
vector<elem> results;
bool elem_less_than(const elem &A, const elem &B)
{
if (A.minlen != B.minlen)
{
return A.minlen > B.minlen;
}
if (A.total != B.total)
{
return A.total < B.total;
}
return A.index < B.index;
}
int getIndex(string str)
{
int ret = 0;
int index = 0;
int num = 0;
if ('G' == str[0])
{
ret = N;
index = 1;
}
for ( ; index < str.size(); ++index)
{
num = 10*num+str[index]-'0';
}
ret += num-1;
return ret;
}
/* dijkstra算法*/
/* 类似临接矩阵的方式记录和处理数据,提高稀疏图下的计算速度*/
void dijkstra(int src)
{
int minIndex;
float mindis = 0xffffff;
int k;
for (int i = 0; i != N+M; ++i)
{
visited[i] = false;
sPath[i] = MAXLEN;
}
sPath[src] = 0;
visited[src] = true;
for (set<int>::iterator iter = graph[src].begin(); iter != graph[src].end(); ++iter)
{
sPath[*iter] = weight[src][*iter];
}
for (int i = 0; i != N+M-1; ++i)
{
mindis = MAXLEN;
for (int j = 0; j != sPath.size(); ++j)
{
if (!visited[j] && sPath[j] < mindis)
{
mindis = sPath[j];
minIndex = j;
}
}
visited[minIndex] = true;
for (set<int>::iterator iter = graph[minIndex].begin(); iter != graph[minIndex].end(); ++iter)
{
k = *iter;
if (!visited[k] && sPath[k] > sPath[minIndex] + weight[minIndex][k])
{
sPath[k] = sPath[minIndex] + weight[minIndex][k];
}
}
}
records.push_back(sPath);
}
int main()
{
string strA, strB;
int ni, nj;
float dis;
float total = 0;
cin >> N >> M >> K >> Ds;
graph.resize(N+M);
sPath.resize(N+M);
for (int i = 0; i != K; ++i)
{
cin >> strA >> strB >> dis;
ni = getIndex(strA);
nj = getIndex(strB);
weight[ni][nj] = weight[nj][ni] = dis;
graph[ni].insert(nj);
graph[nj].insert(ni);
}
/* 计算M个候选位置到各个点的距离*/
for (int i = 0; i != M; ++i)
{
dijkstra(N+i);
}
/* 处理结果*/
for (int i = 0; i < records.size(); ++i)
{
elem temp;
temp.minlen = MAXLEN;
temp.maxlen = 0;
temp.total = 0;
temp.index = i+1;
for (int j = 0; j < N; ++j)
{
if (records[i][j] < temp.minlen)
{
temp.minlen = records[i][j];
}
if (records[i][j] > temp.maxlen)
{
temp.maxlen = records[i][j];
}
temp.total += records[i][j];
}
if (temp.maxlen <= Ds)
{
results.push_back(temp);
}
}
if (0 != results.size())
{
sort(results.begin(), results.end(), elem_less_than);
cout << 'G' << results[0].index << endl;
printf("%0.01f %0.01f\n", results[0].minlen, results[0].total/N);
}
else
{
cout << "No Solution" << endl;
}
return 0;
}