QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#60517#4437. Link with RunningqinjianbinAC ✓772ms19028kbC++6.7kb2022-11-05 10:43:482022-11-05 10:43:49

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-11-05 10:43:49]
  • 评测
  • 测评结果:AC
  • 用时:772ms
  • 内存:19028kb
  • [2022-11-05 10:43:48]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<ctime>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<bitset>
#include<functional>
using namespace std;
//STL库常用(vector, map, set, pair)
#define pii pair<long long,int>
#define pdd pair<double,double>
#define F first
#define S second
//常量
#define PI (acos(-1.0))
#define INF 0x3f3f3f3f3f3f3f3f
//必加
#ifdef TanJI
#include<cassert>
#define dbg(...) do {cerr << #__VA_ARGS__ << " -> "; err(__VA_ARGS__); } while(0)
void err() {cerr << endl;}
template<template<typename...> class T, typename t, typename... A> void err(T<t> a, A... x) {for (auto v: a) cerr << v << ' '; cerr << ", "; err(x...);}
template<typename T, typename... A> void err(T a, A... x) {cerr << a << ' '; err(x...);}
template<typename T> void err(T *a, int len) {for (int i = 0; i < len; i++) cerr << a[i] << ' '; err();}
#else 
#define dbg(...)
#define assert(...)
#endif
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
// 快读快输
template<typename T> inline T read() {T x=0; bool f=0; char c=getchar();while(!isdigit(c)){if(c =='-')f= 1; c=getchar();}while(isdigit(c)){x=(x<<3)+(x<<1)+(c^48);c=getchar();}return f?~x+1:x;}
template<typename T> inline void print(T k){ int num = 0,ch[20]; if(k == 0){ putchar('0'); return ; } (k<0)&&(putchar('-'),k = -k); while(k>0) ch[++num] = k%10, k /= 10; while(num) putchar(ch[num--]+48); }
//图论
const int GRAPHN = 1e5 + 10, GRAPHM = 6e5 + 10;
int h[GRAPHN], e[GRAPHM], ne[GRAPHM], wei[GRAPHM], va[GRAPHM], idx, h1[GRAPHN];
void AddEdge(int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}
void AddEdge(int a, int b, int c, int d) {e[idx] = b, ne[idx] = h[a], wei[idx] = c, va[idx] = d, h[a] = idx++;}
void AddEdge(int h[], int a, int b) {e[idx] = b, ne[idx] = h[a], h[a] = idx++;}
void AddEdge(int h[], int a, int b, int c) {e[idx] = b, ne[idx] = h[a], wei[idx] = c, h[a] = idx++;}
// 数学公式
template<typename T> inline T gcd(T a, T b){ return b==0 ? a : gcd(b,a%b); }
template<typename T> inline T lowbit(T x){ return x&(-x); }
template<typename T> inline bool mishu(T x){ return x>0?(x&(x-1))==0:false; }
template<typename T1,typename T2, typename T3> inline ll q_mul(T1 a,T2 b,T3 p){ ll w = 0; while(b){ if(b&1) w = (w+a)%p; b>>=1; a = (a+a)%p; } return w; }
template<typename T,typename T2> inline ll f_mul(T a,T b,T2 p){ return (a*b - (ll)((long double)a/p*b)*p+p)%p; }
template<typename T1,typename T2, typename T3> inline ll q_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = (w*a)%p; b>>=1; a = (a*a)%p;} return w; }
template<typename T1,typename T2, typename T3> inline ll s_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = q_mul(w,a,p); b>>=1; a = q_mul(a,a,p);} return w; }
template<typename T> inline ll ex_gcd(T a, T b, T& x, T& y){ if(b == 0){ x = 1, y = 0; return (ll)a; } ll r = ex_gcd(b,a%b,y,x); y -= a/b*x; return r;/*gcd*/ }
template<typename T1,typename T2> inline ll com(T1 m, T2 n) { int k = 1;ll ans = 1; while(k <= n){ ans=((m-k+1)*ans)/k;k++;} return ans; }
template<typename T> inline bool isprime(T n){ if(n <= 3) return n>1; if(n%6 != 1 && n%6 != 5) return 0; T n_s = floor(sqrt((db)(n))); for(int i = 5; i <= n_s; i += 6){ if(n%i == 0 || n%(i+2) == 0) return 0; } return 1; }
/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------- */

const int MAXN = 1e5 + 10;

int T, n, m;
ll dist[MAXN];
bool vis[MAXN];
void dijk(int start) {
    for (int i = 1; i <= n; i++) dist[i] = INF, vis[i] = false;
    dist[start] = 0;
    priority_queue<pii, vector<pii>, greater<pii> > que;
    que.push({0, start});
    while (que.size()) {
        int u = que.top().second; que.pop();
        if (vis[u]) continue;
        vis[u] = true;
        for (int i = h[u]; ~i; i = ne[i]) {
            int v = e[i], w = wei[i];
            if (!vis[v] && dist[v] > dist[u] + w) {
                dist[v] = dist[u] + w;
                que.push(make_pair(dist[v], v));
            }
        }
    }
}
int dfn[MAXN], low[MAXN], timestamp = 0;
int stk[MAXN], top;
int scc_cnt;
bool in_stk[MAXN];
int id[MAXN];
void tarjan(int u) {
    dfn[u] = low[u] = ++timestamp;
    stk[++top] = u, in_stk[u]= true;
    for (int i = h[u]; ~i; i = ne[i]) {
        int v = e[i];
        if (dist[v] != dist[u] + wei[i]) continue;
        if (!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if (in_stk[v]) 
            low[u] = min(low[u], dfn[v]);
    }
    if (dfn[u] == low[u]) {
        scc_cnt++;
        int y;
        do {
            y = stk[top--];
            in_stk[y] = false;
            id[y] = scc_cnt;
        } while (y != u);
    }
}
int din[MAXN];
ll dp[MAXN];
void topsort() {
    for (int i = 1; i <= scc_cnt; i++) dp[i] = 0;
    queue<int> que;
    for (int i = 1; i <= scc_cnt; i++) {
        if (!din[i]) que.push(i);
    }
    while (que.size()) {
        int u = que.front(); que.pop();
        dbg(u);
        for (int i = h1[u]; ~i; i = ne[i]) {
            int v = e[i], w = wei[i];
            dp[v] = max(dp[v], dp[u] + w);
            if (--din[v] == 0) {
                que.push(v);
            }
        }
    }
}

int main() {
#ifdef TanJI
    clock_t c1 = clock();
    freopen("D:\\Cpp\\1.in", "r", stdin);
    freopen("D:\\Cpp\\1.out", "w", stdout);
#endif
//--------------------------------------------
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++) h[i] = -1; idx = 0;
        int u, v, w, p;
        for (int i = 1; i <= m; i++) {
            scanf("%d%d%d%d", &u, &v, &w, &p);
            AddEdge(u, v, w, p);
        }
        dijk(1);
        for (int i = 1; i <= n; i++) 
            dfn[i] = low[i] = id[i] = 0;
        timestamp = top = scc_cnt = 0;
        tarjan(1);

        for (int i = 1; i <= scc_cnt; i++) h1[i] = -1;
        for (int u = 1; u <= n ; u++) {
            for (int i = h[u]; ~i; i = ne[i]) {
                int v = e[i];
                if (dist[v] != dist[u] + wei[i]) continue;
                if (id[u] != id[v]) {
                    AddEdge(h1, id[u], id[v], va[i]);
                    dbg(id[u], id[v], va[i]);
                    din[id[v]]++;
                }
            }
        }
        topsort();
        printf("%lld %lld\n", dist[n], dp[id[n]]);
        
    }
//--------------------------------------------    
#ifdef TanJI
    cerr << "Time Used:" << clock() - c1 << "ms" << endl;
#endif
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 772ms
memory: 19028kb

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