-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpat 1018.cpp
110 lines (103 loc) · 3.19 KB
/
pat 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
#include <cstdio>
#include <string.h>
#include <vector>
#include <string>
#define INFI 0x5FFFFFFF
using namespace std;
struct station
{
int length;
vector<int> path;
bool know;
int need;
};
int CMAX,N,Sp,M,mini=0;
station S[501];
int map[501][501]={0};
int min_s = INFI,min_b = INFI;
vector<int> min_p;
void run(int next,int lasts,int lastb,vector<int> pa)
{
pa.push_back(next);
if(next == 0)
if( lasts < min_s || (lasts == min_s && lastb < min_b))
{
min_p = pa;
min_s = lasts;
min_b = lastb;
}
vector<int>::const_iterator p;
int nows=0,nowb=lastb;
if( (lasts + S[next].need) > 0)
nows = lasts + S[next].need;
if( S[next].need < 0 && ((S[next].need + lasts)<0))
nowb = lastb - (S[next].need + lasts);
for( p = S[next].path.begin() ; p != S[next].path.end(); p++)
{
run(*p,nows,nowb,pa);
}
}
int main()
{
int cur,i,st,ed,v;
scanf("%d%d%d%d",&CMAX,&N,&Sp,&M);
CMAX/=2;
for( i = 1; i <=N; i++)
{
scanf("%d",&cur);
S[i].need = CMAX - cur;
S[i].length = INFI;
S[i].know = false;
}
S[0].need = S[0].length = 0;
S[0].know = false;
for(i = 0; i < M; i++)
{
scanf("%d%d",&st,&ed);
scanf("%d",&map[st][ed]);
map[ed][st] = map[st][ed];
}
while(true)
{
int mini_l=INFI;
S[mini].know = true;
if(mini == Sp)
break;
for( v = 1; v <= N; v++)
{
if( map[mini][v]!=0 && !S[v].know )
{
if( S[v].length > S[mini].length + map[mini][v])
{
//update
S[v].length = S[mini].length + map[mini][v];
S[v].path.clear();
S[v].path.push_back(mini);
}
else if( S[v].length == S[mini].length + map[mini][v])
{
S[v].path.push_back(mini);
}
}
}
for( i = 1; i <= N; i++)
{
if( !S[i].know && S[i].length <mini_l )
{
mini_l = S[i].length;
mini= i;
}
}
}
vector<int> pa;
run( Sp,0,0,pa);
printf("%d ",min_s);
vector<int>::const_reverse_iterator p;
for( p = min_p.rbegin();p != min_p.rend(); p++)
{
if(*p != Sp)
printf("%d->",*p);
else
printf("%d %d",Sp,min_b);
}
}