C++
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
struct Edge{
int n;
int dist;
};
struct Q_info{
int n;
int dist;
bool operator < (const Q_info &a) const{
return dist > a.dist;
}
};
vector<Edge> vec[1010];
priority_queue<Q_info> pq;
int N,M;
int result[1010],path[1010];
int path_[1010];
int stop_a=-1,stop_b=-1;
int start,max_dc=0;
int serch(){
for(int i=0;i<1010;i++){
result[i]=INT_MAX;
}
path[1]=-1;
result[1]=0;
pq.push({1,result[1]});
while(!pq.empty()){
Q_info info=pq.top();
pq.pop();
if(info.dist!=result[info.n])
continue;
if(info.n==N)
break;
for(auto p:vec[info.n]){
if((p.n==stop_b && info.n==stop_a)
|| (p.n==stop_a && info.n==stop_b))
continue;
if(result[p.n]>p.dist+info.dist){
result[p.n]=p.dist+info.dist;
path[p.n]=info.n;
pq.push({p.n,result[p.n]});
}
}
}
return result[N];
}
int main()
{
for(int i=0;i<1010;i++){
result[i]=INT_MAX;
}
scanf("%d %d",&N,&M);
for(int i=0;i<M;i++){
int a,b,t;
scanf("%d %d %d",&a,&b,&t);
vec[a].push_back({b,t});
vec[b].push_back({a,t});
}
start=serch();
if(start==INT_MAX){
printf("-1");
return 0;
}
int idx=N;
int i=0;
while(idx!=-1){
path_[i++] = idx;
idx=path[idx];
}
for(int j=1;j<i;j++){
stop_a=path_[j-1];
stop_b=path_[j];
int a=serch();
if(a==INT_MAX){
printf("-1");
return 0;
}
max_dc=max(max_dc,a-start);
}
printf("%d",max_dc);
return 0;
}
댓글 0
번호 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|
공지 | 안내사항 | 관리자 | 2019.12.21 | 163 |
74 | 9663 | 관리자 | 2019.12.21 | 114 |
73 | 9345 | 관리자 | 2020.04.11 | 150 |
72 | 9019 | 관리자 | 2019.12.21 | 113 |
71 | 7569 | 관리자 | 2019.12.21 | 113 |
70 | 6987 | 관리자 | 2019.12.21 | 113 |
69 | 5842 | 관리자 | 2020.04.11 | 151 |
68 | 5625 | 관리자 | 2019.12.21 | 109 |
67 | 5419 | 관리자 | 2020.04.11 | 155 |
66 | 3392 | 관리자 | 2020.04.11 | 156 |
65 | 3019 | 관리자 | 2019.12.21 | 111 |
64 | 2933 | 관리자 | 2019.12.21 | 112 |
63 | 2931 | 관리자 | 2019.12.21 | 111 |
62 | 2836 | 관리자 | 2019.12.21 | 110 |
61 | 2667 | 관리자 | 2019.12.21 | 113 |
60 | 2636 | 관리자 | 2019.12.21 | 113 |
59 | 2629 | 관리자 | 2020.04.11 | 146 |
58 | 2615 | 관리자 | 2019.12.21 | 111 |
57 | 2610 | 관리자 | 2020.04.11 | 146 |
56 | 2606 | 관리자 | 2019.12.21 | 111 |
55 | 2585 | 관리자 | 2019.12.21 | 113 |
54 | 2578 | 관리자 | 2019.12.21 | 111 |
53 | 2573 | 관리자 | 2020.04.11 | 151 |
52 | 2557 | 관리자 | 2019.12.21 | 111 |
51 | 2512 | 관리자 | 2019.12.21 | 111 |
50 | 2504 | 관리자 | 2020.04.11 | 147 |
49 | 2481 | 관리자 | 2019.12.21 | 112 |
48 | 2478 | 관리자 | 2020.04.11 | 148 |
47 | 2473 | 관리자 | 2020.04.11 | 153 |
46 | 2470 | 관리자 | 2020.04.11 | 151 |