QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#114506#4437. Link with RunningLiufAC ✓513ms68656kbC++203.4kb2023-06-22 12:38:512023-06-22 12:38:54

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-22 12:38:54]
  • 评测
  • 测评结果:AC
  • 用时:513ms
  • 内存:68656kb
  • [2023-06-22 12:38:51]
  • 提交

answer

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <cmath>

using namespace std;
#define int long long
#define NO {puts("NO") ; return ;}
#define YES {puts("YES") ; return ;}
#define please return 
#define ac 0
typedef pair<int, int> PII;
const int N = 5e5 + 10 , M = 4 * N , INF = 0x3f3f3f3f3f3f3f3f;
int n, m, a[N];
int h1[N], e[M], ne[M], w1[M], w2[M], h2[N], h3[N], idx;
int dfn[N], low[N], ts, in_stk[N], stk[N], scc_cnt, id[N], top;
int dist[N], din[N], value[N];
bool st[N];
struct Node
{
    int id, w;
    bool operator < (const Node &t) const
    {
        return w > t.w;
    }
};

void add(int h[], int a, int b, int c, int d)
{
    e[idx] = b, w1[idx] = c, w2[idx] = d, ne[idx] = h[a], h[a] = idx ++ ;
}

void dijkstra()
{
    priority_queue<Node> q;
    dist[1] = 0;
    q.push({1, 0});
    while(q.size())
    {
        auto t = q.top();
        q.pop();
        
        int ver = t.id;
        if(st[ver]) continue;
        st[ver] = true;
        
        for(int i = h1[ver] ; ~i ; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > dist[ver] + w1[i])
            {
                dist[j] = dist[ver] + w1[i];
                q.push({j, dist[j]});
            }
        }
    }
    for(int i = 1 ; i <= n ; i ++ )
    {
        if(dist[i] == INF) continue;
        for(int j = h1[i] ; ~j ; j = ne[j])
        {
            int k = e[j];
            if(dist[k] == dist[i] + w1[j])
                add(h2, i, k, w1[j], w2[j]);
        }
    }
}

void tarjan(int u)
{
    low[u] = dfn[u] = ++ ts;
    stk[++ top] = u, in_stk[u] = true;
    for(int i = h2[u]; ~i ; i = ne[i])
    {
        int j = e[i];
        if(!dfn[j])
        {
            tarjan(j);
            low[u] = min(low[u], low[j]);
        }
        else if(in_stk[j]) low[u] = min(low[u], dfn[j]);
    }
    if(low[u] == dfn[u])
    {
        int y;
        ++ scc_cnt;
        do
        {
            y = stk[top -- ];
            id[y] = scc_cnt;
            in_stk[y] = false;
        }while(y != u);
    }
}

void topsort()
{
    queue<int> q;
    q.push(id[1]);
    value[id[1]] = 0;
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        
        for(int i = h3[t] ; ~i ; i = ne[i])
        {
            int j = e[i];
            value[j] = max(value[j], value[t] + w2[i]);
            if(-- din[j] == 0) q.push(j);
        }
    }
}

void solve()
{
    cin >> n >> m;
    idx = scc_cnt = top = 0;
    for(int i = 0 ; i <= n + 10 ; i ++ ) 
    {
        h1[i] = h2[i] = h3[i] = -1;
        st[i] = dfn[i] = in_stk[i] = stk[i] = din[i] = id[i] = value[i] = low[i] = 0;
        dist[i] = INF;
    }
    for(int i = 1 ; i <= m ; i ++ )
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        add(h1, a, b, c, d);
    }
    dijkstra();
    for(int i = 1 ; i <= n ; i ++ )
        if(!dfn[i]) tarjan(i);
    
    for(int i = 1 ; i <= n ; i ++ )
    {
        for(int j = h2[i] ; ~j ; j = ne[j])
        {
            int a = id[i], b = id[e[j]];
            if(a != b) din[b] ++ , add(h3, a, b, w1[j], w2[j]);
        }
    }
    topsort();
    cout << dist[n] << " " << value[id[n]] << endl;
}

signed main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    int T = 1;
    cin >> T;
    while(T -- ) solve();
    please ac;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 513ms
memory: 68656kb

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