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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 | #include<bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
const int N = 1e5 + 5, M = 3e5 + 5, inf = LLONG_MAX / 2;
typedef pair<int, int> PII;
int head1[N] ,e1[M], ne1[M], w1[M], w2[M], idx1 = 0;
int head2[N] ,e2[M], ne2[M], ww1[M], ww2[M], idx2 = 0;
bool vis[N];
int n, m;
int dis1[N], dis2[N];
int a, b, c, d;
void add1(int a, int b, int c, int d) {
e1[idx1] = b;
w1[idx1] = c;
w2[idx1] = d;
ne1[idx1] = head1[a];
head1[a] = idx1 ++ ;
}
void add2(int a, int b, int c, int d) {
e2[idx2] = b;
ww1[idx2] = c;
ww2[idx2] = d;
ne2[idx2] = head2[a];
head2[a] = idx2 ++ ;
}
void djs(int s, int t) {
for(int i = 1; i <= n; i ++ ) dis1[i] = inf;
memset(vis, false, sizeof(vis));
dis1[s] = 0;
priority_queue<PII, vector<PII>, greater<PII> > q;
q.push({0, s});
while(q.size()) {
auto cur = q.top();
q.pop();
int pos = cur.y;
if(vis[pos]) continue;
vis[pos] = true;
for(int i = head1[pos]; i != -1; i = ne1[i]) {
int j = e1[i];
if(dis1[j] > dis1[pos] + w1[i]) {
dis1[j] = dis1[pos] + w1[i];
q.push({dis1[j], j});
}
}
}
}
int dfn[N], low[N], timestamp = 0;
bool is_loop[N];
int scc_cnt = 0;
int id[N];
int in[N];
int stk[N], tt = 0;
bool in_stk[N];
void tarjan(int u) { // tarjan缩点
dfn[u] = low[u] = ++ timestamp;
stk[++ tt] = u;
in_stk[u] = true;
for(int i = head2[u]; i != -1; i = ne2[i]) {
int j = e2[i];
if(!dfn[j]) {
tarjan(j);
low[u] = min(low[u], low[j]);
} else if(in_stk[j]){
low[u] = min(low[u], low[j]);
}
}
if(dfn[u] == low[u]) {
int y;
scc_cnt ++ ;
do {
y = stk[tt -- ];
in_stk[y] = false;
id[y] = scc_cnt;
} while(y != u);
}
}
void clear() {
memset(head2, -1, sizeof(head2));
memset(head1, -1, sizeof(head1));
memset(is_loop, false, sizeof(is_loop));
memset(in_stk, false, sizeof(in_stk));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(in, 0, sizeof(in));
idx1 = 0 ;
idx2 = 0;
timestamp = 0;
tt = 0;
scc_cnt = 0;
}
void build() {
for(int i = 1; i <= n; i ++ ) { // 建最短路图
if(dis1[i] == inf) continue;
for(int j = head1[i]; j != -1; j = ne1[j]) {
int k = e1[j];
if(dis1[k] == dis1[i] + w1[j]) {
add2(i, k, w1[j], w2[j]);
}
}
}
for(int i = 1; i <= n; i ++ ) {
if(!dfn[i]) {
tarjan(i);
}
}
memset(head1, -1, sizeof(head1));
idx1 = 0;
for(int i = 1; i <= n; i ++ ) {
for(int j = head2[i]; j != -1; j = ne2[j]) {
int k = e2[j];
int a = id[i], b = id[k];
if(a == b) {
if(ww2[j] > 0)
is_loop[a] = true; // 找正环,题目保证不存在
} else {
add1(a, b, ww1[i], ww2[j]);
in[b] ++ ;
}
}
}
}
void dp() {
for(int i = scc_cnt; i >= 1; i -- ) {
dis2[i] = -inf;
}
int s = id[1];
dis2[s] = 0;
queue<int> q;
for(int i = 1; i <= scc_cnt; i ++ ) {
if(in[i] == 0) {
q.push(i);
in[i] = -1;
}
}
while(q.size()) {
int pos = q.front();
q.pop();
if(is_loop[pos]) dis2[pos] = inf;
for(int i = head1[pos]; i != -1; i = ne1[i]) {
int j = e1[i];
dis2[j] = max(dis2[j], dis2[pos] + w2[i]);
in[j] -- ;
if(!in[j]) {
q.push({j});
in[j] = -1;
}
}
}
}
void solve() {
int res1 = 0, res2 = 0;
cin >> n >> m;
memset(head1, -1, sizeof(head1));
memset(head2, -1, sizeof(head2));
idx1 = 0, idx2 = 0;
for(int i = 1; i <= m; i ++ ) {
cin >> a >> b >> c >> d;
add1(a, b, c, d);
}
djs(1, n);
res1 = dis1[n];
build();
dp(); // 拓扑排序找最长路
res2 = dis2[id[n]];
cout << res1 << " " << res2 << '\n';
clear();
}
signed main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int t;
cin >> t;
while(t -- ) {
solve();
}
return 0;
}
|