-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdu_2263_Cycling.cpp
126 lines (126 loc) · 3.02 KB
/
hdu_2263_Cycling.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
#include"iostream"
#include"queue"
#include"algorithm"
#include"cstdio"
#include"string.h"
#define MAXN 105
#define MAXM 10005
#define inf 0x3f3f3f3f
using namespace std;
struct ee{
int u,v,c;
}edge[MAXM];
struct edges{
int u,c,next;
}e[MAXM];
int N,M,idx;
int p[MAXN],dist[MAXN];
bool used[MAXN];
queue<int>q;
void init(){
idx=0;
memset(p,0xff,sizeof(p));
}
void addedge(int u,int v,int c){
e[idx].u=v;
e[idx].c=c;
e[idx].next=p[u];
p[u]=idx++;
}
void spfa(int s){
int t,u,w;
while(!q.empty())q.pop();
memset(used,false,sizeof(used));
for(int i=0;i<N;i++)dist[i]=inf;
dist[s]=0;
q.push(s);
while(!q.empty()){
t=q.front();
q.pop();
used[t]=false;
for(int i=p[t];i!=-1;i=e[i].next){
u=e[i].u;
w=e[i].c;
if(dist[t]+w<dist[u]){
dist[u]=dist[t]+w;
if(!used[u]){
used[u]=true;
q.push(u);
}
}
}
}
}
int hash[MAXN];
struct node{
int id,height;
friend bool operator<(node a,node b){
return a.height<b.height;
}
}nod[MAXN];
int main(){
int T,a,b,K,l,h,ll,hh,mid,low,mh,lh;
scanf("%d",&T);
while(T--){
scanf("%d%d",&N,&M);
mh=inf;
lh=0;
for(int i=0;i<N;i++){
scanf("%d",&nod[i].height);
if(nod[i].height<mh)mh=nod[i].height;
if(nod[i].height>=lh)lh=nod[i].height;
nod[i].id=i;
}
sort(nod,nod+N);
for(int i=0;i<N;i++){
hash[nod[i].id]=i;
}
for(int i=0;i<M;i++){
scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].c);
edge[i].u--;
edge[i].v--;
}
bool flag=false;
int res=-1;
h=lh-mh;
l=0;
res=inf;
int rd=inf;
while(l<=h){
flag=false;
mid=(l+h)>>1;
for(int i=0;i<N;i++){
low=nod[i].height;
init();
for(int j=0;j<M;j++){
if(nod[hash[edge[j].u]].height<low ||
nod[hash[edge[j].u]].height>low+mid ||
nod[hash[edge[j].v]].height<low ||
nod[hash[edge[j].v]].height>low+mid)continue;
addedge(edge[j].u,edge[j].v,edge[j].c);
addedge(edge[j].v,edge[j].u,edge[j].c);
}
spfa(0);
if(dist[N-1]!=inf){
if(mid==res){
if(dist[N-1]<rd)rd=dist[N-1];
}
else if(mid<res){
res=mid;
rd=dist[N-1];
}
flag=true;
}
}
if(flag){
h=mid-1;
}
else{
l=mid+1;
}
}
printf("%d %d\n",res,rd);
}
// system("pause");
return 0;
}