QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#340785 | #4437. Link with Running | Terac# | AC ✓ | 383ms | 91952kb | C++14 | 3.8kb | 2024-02-29 12:02:27 | 2024-03-01 17:29:48 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
namespace IO {
#if ONLINE_JUDGE
#define getc() (IS == IT && (IT = (IS = ibuf) + fread(ibuf, 1, IL, stdin), IS == IT) ? EOF : *IS++)
#else
#define getc() getchar()
#endif
const int IL = 1 << 21, OL = 1 << 21;
int olen = 0;
char ibuf[IL], *IS = ibuf, *IT = ibuf, obuf[OL];
inline int read() {
register char ch = getc(); register int x = 0, f = 1;
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getc(); }
while(isdigit(ch)) x = x * 10 + ch - 48, ch = getc();
return x * f;
}
inline double readdb() {
register char ch = getc(); register double x = 0, f = 1;
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getc(); }
while(isdigit(ch)) x = x * 10 + ch - 48, ch = getc();
if(ch == '.') {
register double b = 0.1;
ch = getc();
while(isdigit(ch)) x += (ch - 48) * b, b *= 0.1, ch = getc();
}
return x * f;
}
inline int readstr(char *s) {
register char ch = getc(); register int len = 0;
while(!isalpha(ch)) ch = getc();
while(isalpha(ch)) s[++len] = ch, ch = getc();
return len;
}
inline void flush() { fwrite(obuf, 1, olen, stdout); olen = 0; }
inline void putc(register char ch) { obuf[olen++] = ch; }
template<class T>
inline void write(register T x) {
if(x < 0) obuf[olen++] = '-', x = -x;
if(x > 9) write(x / 10);
obuf[olen++] = x % 10 + 48;
}
} using namespace IO;
const int N = 3e5 + 10;
int n, m;
struct edge {
int to, w, c, nxt;
} e[N];
int C[N], U[N], V[N];
int head[N], ecnt;
void addedge(int u, int v, int w, int c) {
e[++ecnt] = {v, w, c, head[u]};
head[u] = ecnt;
}
int dis[N];
struct node {
int dis, u;
bool operator < (const node &y) const {
return dis > y.dis;
}
};
bool vis[N];
void dij() {
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
priority_queue<node> q;
q.push((node){0, 1});
dis[1] = 0;
while(!q.empty()) {
int u = q.top().u;
q.pop();
vis[u] = 0;
for(int i = head[u], v; i; i = e[i].nxt) {
v = e[i].to;
if(dis[v] > dis[u] + e[i].w) {
dis[v] = dis[u] + e[i].w;
if(!vis[v]) vis[v] = 1, q.push({dis[v], v});
}
}
}
}
int dfn[N], tot, db[N], bel[N], low[N], tim;
bool ist[N];
stack<int> st;
void tarjan(int u) {
dfn[u] = low[u] = ++tim;
ist[u] = 1;
st.push(u);
for(int i = head[u], v; i; i = e[i].nxt) {
v = e[i].to;
if(dis[v] != dis[u] + e[i].w) continue;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(ist[v]) low[u] = min(low[u], dfn[v]);
}
if(low[u] == dfn[u]) {
int now;
++tot;
do {
now = st.top();
ist[now] = 0;
bel[now] = tot;
st.pop();
} while(now != u);
db[tot] = u;
}
}
vector<vector<int> > E[N];
int dis2[N], W[N];
bool vis2[N];
void dfs(int x) {
dis2[x] = -1e15;
if(x == bel[n]) dis2[x] = 0;
vis2[x] = 1;
for(auto k : E[x]) {
if(dis[db[k[0]]] != dis[db[x]] + k[1]) continue;
if(!vis2[k[0]]) dfs(k[0]);
if(dis2[k[0]] + k[2] > dis2[x])
dis2[x] = dis2[k[0]] + k[2];
}
}
void MAIN() {
n = read(), m = read();
ecnt = tim = tot = 0;
memset(head, 0, sizeof(head));
memset(dfn, 0, sizeof(dfn));
memset(vis2, 0, sizeof(vis2));
memset(dis2, -0x3f, sizeof(dis2));
for(int i = 1; i <= n; i++)
E[i].clear();
for(int i = 1, u, v, e, p; i <= m; i++)
u = read(), v = read(), e = read(), p = read(), addedge(u, v, e, p),
U[i] = u, V[i] = v, W[i] = e, C[i] = p;
dij();
for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i);
// return;
for(int i = 1; i <= m; i++) {
if(U[i] == V[i]) continue;
if(bel[U[i]] != bel[V[i]] && dis[V[i]] == dis[U[i]] + W[i])
E[bel[U[i]]].push_back({bel[V[i]], W[i], C[i]});
// else assert(!W[i] && !C[i]);
}
dfs(bel[1]);
printf("%lld %lld\n", dis[n], dis2[bel[1]]);
}
signed main() {
int T = read();
while(T--) MAIN();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 383ms
memory: 91952kb
input:
12 100000 200000 1 2 838279516 902819511 1 3 293478832 513256010 2 4 682688353 204481674 2 5 360092507 651108247 5 6 519851939 323803002 6 7 675439277 205804465 7 8 419167205 386168059 6 9 140767493 382483305 9 10 558115401 613738466 9 11 902235661 744659643 9 12 851394758 1720015 12 13 635355827 46...
output:
5927443549 11285847934 2529348 325344428756 2522027 438209666288 250100947 25049026205784 249512452 24966236662852 0 9535132634 0 25375698217 0 1000000000 0 1 0 2 0 1 0 1
result:
ok 12 lines