-
Notifications
You must be signed in to change notification settings - Fork 0
/
1018.cpp
169 lines (157 loc) · 3.38 KB
/
1018.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
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#define MY_INT_MAX 1000000000
using namespace std;
#define DEBUG
int n,cmax,sp,m;
int map[501][501];
int bike[501];
bool inS[501];
vector<vector<int>> all_paths[501];
int length[501];
int main()
{
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
//input
scanf("%d%d%d%d",&cmax,&n,&sp,&m);
int i,j,from,to,time;
for(i=1;i<=n;i++)
scanf("%d",&bike[i]);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
map[i][j]=MY_INT_MAX;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&from,&to,&time);
map[from][to]=time;
map[to][from]=time;
}
//shortest path
for(i=1;i<=n;i++)
{
inS[i]=false;
length[i]=map[0][i];
if(length[i]<MY_INT_MAX)
{
vector<int> v;
v.push_back(i);
vector<vector<int>> vv;
vv.push_back(v);
all_paths[i]=vv;
}
}
inS[0]=true;
int min;
int minV;
while(!inS[sp])
{
minV=MY_INT_MAX;
for(i=1;i<=n;i++)
{
if(!inS[i] && length[i]<minV)
{
min=i;
minV=length[i];
}
}
#ifdef DEBUG
int debug_i;
for(debug_i=1;debug_i<=n;debug_i++)
{
printf("length[%d]=%d ",debug_i,length[debug_i]);
}
printf("\nin this iteration, min=%d, length[min]=%d\n",min,length[min]);
#endif
inS[min]=true;
for(i=1;i<=n;i++)
{
if(inS[i])
continue;
#ifdef DEBUG
printf("now i=%d, length[i]=%d\n",i,length[i]);
#endif
if(length[i]>map[min][i]+length[min])
{
#ifdef DEBUG
printf("find a new short path to %d\n",i);
#endif
length[i]=map[min][i]+length[min];
all_paths[i].clear();
for(vector<vector<int>>::iterator it=all_paths[min].begin();it!=all_paths[min].end();it++)
{
vector<int> nv= *it;
nv.push_back(i);
all_paths[i].push_back(nv);
}
}
else if(length[i]==map[min][i]+length[min])
{
#ifdef DEBUG
printf("find an equal path to %d\n",i);
#endif
for(vector<vector<int>>::iterator it=all_paths[min].begin();it!=all_paths[min].end();it++)
{
vector<int> nv= *it;
nv.push_back(i);
all_paths[i].push_back(nv);
}
}
}
}
int minSend=99999999;
int send;
int record_send;
vector<vector<int>> possible_paths;
//find the paths with less send
for(vector<vector<int>>::iterator it=all_paths[sp].begin();it!=all_paths[min].end();it++)
{
send=0;
record_send=0;
for(vector<int>::iterator jt=it->begin();jt!=it->end();jt++)
{
send+=cmax/2-bike[*jt];
if(send>record_send)
record_send=send;
}
#ifdef DEBUG
printf("record_send=%d, minSend=%d\n",record_send,minSend);
#endif
if(record_send<minSend)
{
minSend=record_send;
possible_paths.clear();
possible_paths.push_back(*it);
}
else if(record_send == minSend)
{
possible_paths.push_back(*it);
}
}
int store;
int minStore=99999999;
vector<vector<int>>::iterator bestPath;
for(vector<vector<int>>::iterator it=possible_paths.begin();it!=possible_paths.end();it++)
{
store=minSend;
for(vector<int>::iterator jt=it->begin();jt!=it->end();jt++)
{
store=store-(cmax/2-bike[*jt]);
}
#ifdef DEBUG
printf("this path, store=%d\n",store);
#endif
if(store<minStore)
{
minStore=store;
bestPath=it;
}
}
printf("%d 0",minSend);
for(vector<int>::iterator jt=bestPath->begin();jt!=bestPath->end();jt++)
printf("->%d",*jt);
printf(" %d",minStore);
return 0;
}