-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11463.cpp
133 lines (126 loc) · 3.01 KB
/
11463.cpp
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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <bitset>
using namespace std;
#define MAX 105
#define INF 1<<20
typedef pair< int , int > ii;
typedef pair< int , ii > iii;
bitset< MAX > visited;
int graph[ MAX ][ MAX ];
int path[ MAX ][ MAX ];
int ff[ MAX ];
vector< int > res;
bool fres;
int n,r;
void floyd()
{
int dt;
for( int k = 0 ; k < n ; ++k )
for( int i = 0 ; i < n ; ++i )
for( int j = 0 ; j < n ;++j )
{
dt = graph[ i ][ k ] + graph[ k ][ j ];
if( graph[ i ][ j ] > dt )
{
graph[ i ][ j ] = dt ;
path[ i ][ j ] = path[ i ][ k ];
}
}
}
int re( int i , int j )
{
// cout << i << " " << j << " " << visited[ i ]<< " " << visited[ j ] << endl;
if( visited[ i ] )
return i;
int s = i;
if( i != j ) s = re( path[ i ][ j ] , j );
if( fres )
res.push_back( i );
visited[ i ] = 1;
return s;
}
int main()
{
int t,c;
int a,b;
c = 0;
scanf("%d",&t);
while( t-- )
{
memset( ff , -1 , sizeof ff );
scanf("%d %d",&n ,&r);
for( int i = 0 ; i < n ; ++i )
for( int j = 0 ; j < n ; ++j )
{
if( j == i )
graph[ i ][ j ] = 0;
else
graph[ i ][ j ] = INF;
path[ i ][ j ] = j;
}
for( int i = 0 ; i < r ; ++i )
{
scanf("%d %d",&a,&b);
graph[ a ][ b ] = min(1,graph[a][b]);
graph[ b ][ a ] = min(1,graph[b][a]);
}
scanf("%d %d",&a,&b );
floyd();
visited.reset();
int totW = 0;
// cout << a << " " << b << " " << graph[ a ][ b ] << endl;
totW += graph[ a ][ b ];
fres = true;
res.clear();
re( a , b );
fres = false;
/*
// cout << a << " "<< b << endl;
// cout << "hola" << endl;
int may = -1, tt = 0;
for( int i = 0 ; i < n ; ++i )
{
temp = i;
if( ! visited[ i ] )
{
temp = re( i , b );
// cout << temp << endl;
// totW += 2*graph[ i ][ temp ];
// ff[ temp ] = max( ff[ temp ] , 2*graph[ i ][ temp ] );
// may = max( may , graph[ i ][ temp ] + min(graph[ i ][ temp ] , graph[ i ][ b ] ) );
tt = graph[ i ][ b ] - graph[ temp ][ b ];
if ( tt < 0 )
tt = 0;
cout << "hk "<< tt << " i " << i << " " << graph[ i ][ b ] << " " << graph[ temp ][ b ] << " temp " << temp<< endl;
may = max( may , graph[ i ][ temp ] + ( min ( graph[ i ][ temp ] , tt )));
}
// cout << totW << endl;
}
*/
int may = 0, temp = 0 ;
for( int i = 0; i < n ; ++i )
{
if( !visited[ i ] )
{
temp = INF;
for( int j = 0 ; j < res.size() ; ++j )
{
int tt ;
tt = graph[ i ][ b ] - graph[ res[ j ] ][ b ];
if( tt < 0 )
tt = 0;
temp = min( temp , graph[ i ][ res[ j ] ] + min( graph[ i ][ res [ j ] ] , tt ));
}
may = max( may , temp );
}
// cout << "llegue" << endl;
}
totW += may;
printf("Case %d: %d\n",++c,totW );
}
return 0;
}