-
Notifications
You must be signed in to change notification settings - Fork 0
/
team10_1011.cpp
101 lines (91 loc) · 2 KB
/
team10_1011.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
/*************************************************************************
> File Name: team10_1011.cpp
> Author: heheql
> Mail: 374655767@qq.com
> Created Time: 2017年08月24日 星期四 16时04分20秒
************************************************************************/
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<functional>
using namespace std;
const int maxn = 100100;
const long long inf = 1e15;
typedef pair<long long, int> P;
struct edge {
int to;
long long cost;
};
int n, m;
long long ans;
long long dis1[maxn], disn[maxn], sumt[maxn];
void Dijkstra(int src, long long dis[], vector<edge> G[])
{
priority_queue< P, vector<P>, greater<P> >que;
dis[src] = 0, sumt[src] = 1;
que.push(P(dis[src], src));
while (!que.empty()) {
P p = que.top();
que.pop();
int u = p.second;
long long d = p.first;
if (d>dis[u]) continue;
for (int i = 0; i<G[u].size(); i++) {
edge e = G[u][i];
if (e.cost + d<dis[e.to]) {
sumt[e.to] = sumt[u];
dis[e.to] = d + e.cost;
que.push(P(dis[e.to], e.to));
}
else if (e.cost + d == dis[e.to]) {
sumt[e.to] += sumt[u];
}
}
}
}
void solve(vector<edge> G[])
{
fill(dis1 + 1, dis1 + 1 + n, inf);
fill(disn + 1, disn + 1 + n, inf);
fill(sumt + 1, sumt + 1 + n, 0);
Dijkstra(1, dis1, G);
if (sumt[n] > 1) {
ans = dis1[n];
return;
}
Dijkstra(n, disn, G);
ans = 1e15;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < G[i].size(); j++) {
int v = G[i][j].to, d = G[i][j].cost;
if (dis1[i] + d + disn[v] > dis1[n]) {
ans = min(ans, (long long)(dis1[i] + d + disn[v]));
}
}
}
}
int main()
{
// freopen("input.txt", "r", stdin);
int t;
scanf("%d", &t);
while (t--) {
vector<edge> G[maxn];
scanf("%d%d", &n, &m);
int u, v;
long long w;
edge e;
for (int i = 1; i <= m; i++) {
scanf("%d%d%lld", &u, &v, &w);
e.to = v, e.cost = w;
G[u].push_back(e);
e.to = u;
G[v].push_back(e);
}
solve(G);
printf("%lld\n", ans);
}
return 0;
}