QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#107004#853. Flat OrganizationSortingWA 12ms3996kbC++203.7kb2023-05-20 06:22:482023-05-20 06:22:50

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-20 06:22:50]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:3996kb
  • [2023-05-20 06:22:48]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int MAXN = 2007 ;
const int inf = 2e9 + 7 ;
const ll big_inf = 2e18 + 7 ;

int n ;
int ori[ MAXN ][ MAXN ] ;
int used[ MAXN ] , comp[ MAXN ] ;
stack < int > s ;

int st , en ;
int a[ MAXN ][ MAXN ] ;
pii best_edge[ MAXN ][ MAXN ] ;

void dfs ( int x ) {
    used[ x ] = 1 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( ori[ x ][ i ] > 0 && used[ i ] == 0 ) {
            dfs ( i ) ;
        }
    }
    s.push ( x ) ;
}

void rev_dfs ( int x , int wh ) {
    comp[ x ] = wh ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( ori[ i ][ x ] > 0 && comp[ i ] == 0 ) {
            rev_dfs ( i , wh ) ;
        }
    }
}

ll dist[ MAXN ] , prv[ MAXN ] , aux[ MAXN ] ;
bool done[ MAXN ] ;


void solve ( ) {
    cin >> n ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        for ( int j = 1 ; j <= n ; ++ j ) {
            cin >> ori[ i ][ j ] ;
        }
    }
    if ( n == 2 ) {
        cout << "NO\n" ;
        return ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        used[ i ] = comp[ i ] = 0 ;
        for ( int j = 1 ; j <= n ; ++ j ) {
            a[ i ][ j ] = inf ;
            best_edge[ i ][ j ] = { 0 , 0 } ;
        }
    }
    st = en = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( used[ i ] == 0 ) {
            dfs ( i ) ;
        }
    }
    int tp = 0 ; 
    while ( s.empty ( ) == false ) {
        int x = s.top ( ) ;
        s.pop ( ) ;
        if ( comp[ x ] != 0 ) { continue ; }
        rev_dfs ( x , ++ tp ) ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        for ( int j = 1 ; j <= n ; ++ j ) {
            if ( comp[ i ] == comp[ j ] ) { continue ; }
            if ( ori[ i ][ j ] > 0 && a[ comp[ i ] ][ comp[ j ] ] > ori[ i ][ j ] ) {
                a[ comp[ i ] ][ comp[ j ] ] = ori[ i ][ j ] ;
                best_edge[ comp[ i ] ][ comp[ j ] ] = { i , j } ;
            }
        }
    }
    for ( int i = 1 ; i <= tp ; ++ i ) {
        int in_cnt = 0 , out_cnt = 0 ;
        for ( int j = 1 ; j <= tp ; ++ j ) {
            if ( a[ i ][ j ] != inf ) { ++ out_cnt ; }
            if ( a[ j ][ i ] != inf ) { ++ in_cnt ; }
        }
        if ( in_cnt == 0 ) { st = i ; }
        if ( out_cnt == 0 ) { en = i ; }
    }
    for ( int i = 1 ; i <= tp ; ++ i ) {
        dist[ i ] = big_inf ;
        prv[ i ] = aux[ i ] = 0 ;
        done[ i ] = false ;
    }
    dist[ st ] = 0 ;
    while ( 1 ) {
        ll opt = big_inf ;
        int x ;
        for ( int i = 1 ; i <= tp ; ++ i ) {
            if ( done[ i ] == true ) { continue ; }
            if ( opt > dist[ i ] ) {
                opt = dist[ i ] ;
                x = i ;
            }
        }
        if ( opt == big_inf ) { break ; }
        done[ x ] = true ;
        for ( int i = 1 ; i <= tp ; ++ i ) {
            if ( a[ x ][ i ] != inf && dist[ i ] > dist[ x ] + a[ x ][ i ] ) {
                aux[ i ] = aux[ x ] + 1 ;
                dist[ i ] = dist[ x ] + a[ x ][ i ] ;
                prv[ i ] = x ;
            }
        }
    }
    cout << "YES\n" ;
    cout << aux[ en ] << " " << dist[ en ] << "\n" ;
    vector < pii > ans ;
    while ( en != st ) {
        ans.push_back ( best_edge[ prv[ en ] ][ en ] ) ;
        en = prv[ en ] ;
    }
    reverse ( ans.begin ( ) , ans.end ( ) ) ;
    for ( auto [ x , y ] : ans ) {
        cout << x << " " << y << "\n" ;
    }
}


int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; cin >> t ;
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3560kb

input:

1
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0

output:

YES
2 10
1 4
4 5

result:

ok OK!

Test #2:

score: -100
Wrong Answer
time: 12ms
memory: 3996kb

input:

100
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0
1
0
2
0 0
7 0
2
0 1000000000
0 0
3
0 0 5
6 0 0
0 7 0
3
0 4 6
0 0 0
0 1 0
3
0 4 0
0 0 0
6 3 0
3
0 0 0
10 0 6
3 0 0
3
0 1999999998 1999999999
0 0 1999999998
0 0 0
50
0 105800 801 1800 64800 0 259200 288801 72201 12801 125000 20001 28801 33800 ...

output:

YES
2 10
1 4
4 5
YES
0 0
NO
NO
YES
0 0
YES
1 4
1 2
YES
1 3
3 2
YES
2 9
2 3
3 1
YES
1 1999999999
1 3
YES
3 602
4 33
11 47
34 39
YES
3 649
27 12
32 45
17 29
YES
5 1209
1 18
14 35
35 4
4 25
25 12
YES
3 844
3 8
1 41
14 21
YES
3 866
17 35
35 44
46 26
YES
4 1066
10 28
36 24
24 2
37 8
YES
3 1122
4 17
43 19...

result:

wrong answer Test 48: The solution is not optimal