-
Notifications
You must be signed in to change notification settings - Fork 0
/
1072.cpp
148 lines (139 loc) · 2.68 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
#include <bits/stdc++.h>
using namespace std;
const int maxv = 1100;//题目说是小于等于10^3但不能设为120,否则最后一个测试点段错误,设为1000最后一个测试点答案错误
const int inf = 0x3fffffff;
int n, m, k, ds;
int stringtoint(string s){
if(s[0] == 'G'){
return n + stoi(s.substr(1));
}else{
return stoi(s);
}
}
string inttostring(int i){
if(i > n){
string s1 = "G";
string s2 = to_string(i - n);
return s1 + s2;
}else{
return to_string(i);
}
}
int G[maxv][maxv];
bool vis[maxv] = {false};
int d[20][maxv];
void Dijkstra(int s){
int x = s - n;
fill(d[x], d[x]+maxv, inf);
fill(vis, vis+maxv, false);
d[x][s] = 0;
for(int i=1; i<=n+m; i++){
int u = -1, min = inf;
for(int j=1; j<=n+m; j++){
if(!vis[j] && d[x][j]<min){
u = j;
min = d[x][j];
}
}
if(u == -1) return;
vis[u] = true;
for(int v=1; v<=n+m; v++){
if(!vis[v] && G[u][v]!=inf && d[x][u]+G[u][v]<d[x][v]){
d[x][v] = d[x][u] + G[u][v];
}
}
}
}
vector<int> satisfied;
int minDistance(int x){
int min = inf;
for(int i=1; i<=n; i++){
if(d[x][i] < min){
min = d[x][i];
}
}
return min;
}
int selectAnswer(){
for(int i=1; i<=m; i++){
bool flag = true;
for(int j=1; j<=n; j++){
if(d[i][j] > ds){
flag = false;
break;
}
}
if(flag){
satisfied.push_back(i);
}
}
if(satisfied.size() == 0){
return -1;
}else if(satisfied.size() == 1){
return satisfied[0] + n;
}
vector<int> tempSatis;
int far = -1;
for(auto i : satisfied){
int mindis = minDistance(i);
if(mindis > far){
far = mindis;
tempSatis.clear();
tempSatis.push_back(i);
}else if(mindis == far){
tempSatis.push_back(i);
}
}
satisfied = tempSatis;
if(satisfied.size() == 1){
return satisfied[0] + n;
}
double minavg = inf;
for(auto i : satisfied){
double avg = 0;
for(int j=1; j<=n; j++){
avg += d[i][j];
}
avg /= n;
if(avg < minavg){
minavg = avg;
tempSatis.clear();
tempSatis.push_back(i);
}else if(avg == minavg){
tempSatis.push_back(i);
}
}
return tempSatis[0] + n;
}
int main(){
ios::sync_with_stdio(false);
fill(G[0], G[0]+maxv*maxv, inf);//主函数最开始要对图初始化
cin>>n>>m>>k>>ds;
for(int i=0; i<k; i++){
int dist, u, v;
string p1, p2;
cin>>p1>>p2>>dist;
u = stringtoint(p1);
v = stringtoint(p2);
G[u][v] = G[v][u] = dist;
}
for(int i=1; i<=m; i++){
Dijkstra(n + i);
}
int answer = selectAnswer();
if(answer == -1){
cout<<"No Solution";
}else{
cout<<inttostring(answer)<<endl;
double minimum = inf, average = 0;
int x = answer - n;
for(int i=1; i<=n; i++){
average += d[x][i];
if(d[x][i] < minimum){
minimum = d[x][i];
}
}
average /= n;
cout<<fixed<<setprecision(1)<<minimum<<' '<<round(average*10)/10;
}
}