-
Notifications
You must be signed in to change notification settings - Fork 0
/
loj1174.cpp
167 lines (124 loc) · 2.6 KB
/
loj1174.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using namespace std;
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#define max(a,b) ((a>b)? (a):(b))
vector<int>graph[105];
bool visited[105];
int bfs(int s,int d)
{
if(s==d)
return 0;
memset(visited,false,sizeof(visited));
int cost[105],i,u;
queue<int>Q;
visited[s]=true;
Q.push(s);
cost[s]=0;
while(!Q.empty())
{
u = Q.front();
Q.pop();
for(i=0;i<graph[u].size();i++)
{
if(visited[graph[u][i]]==false)
{
visited[graph[u][i]]=true;
if(graph[u][i]==d)
return cost[u]+1;
cost[graph[u][i]]=cost[u]+1;
Q.push(graph[u][i]);
}
}
}
}
int main()
{
int t,u,v,e,n,s,d,Case=1,i,dst1[105],dst2[105];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&e);
for(i=0;i<e;i++)
{
scanf("%d%d",&u,&v);
graph[u].push_back(v);
graph[v].push_back(u);
}
scanf("%d%d",&s,&d);
int ans=0,x,y;
for(i=0;i<n;i++)
{
x=bfs(s,i);
y=bfs(d,i);
// cout<<x<<" "<<y<<endl;
ans=max(ans,x+y);
}
printf("Case %d: %d\n",Case++,ans);
for(i=0;i<=n;i++)
graph[i].clear();
}
return 0;
}
/*using namespace std;
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
#include<cstdio>
#include <utility>
#define INF 100000500
#define min(a,b) ((a<b)?(a):(b))
vector<int>edge[205];
int cost[205],N,i;
void dijkstra (int s) {
priority_queue<int>Q;
for(int i=0;i<N;i++)
cost[i] = INF;
cost[s] = 0;
Q.push(s);
while(!Q.empty())
{
long long int u = Q.top();
// cout<<u<<endl;
Q.pop();
for(i=0;i<edge[u].size();i++)
{
long long int v = edge[u][i];
if(cost[v]>(1+cost[u]))
{
cost[v]=1+cost[u];
Q.push(v);
}
}
}
}
int cost_counter(int x)
{
if(x==0)
return 0;
return cost[x]+cost_counter(x-1);
}
int main()
{
int T,e,ans,Case=1,u,v;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&N,&e);
for(i=0;i<=e;i++)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
dijkstra(0);
ans=0;
for(i=N-1;i>0;i--)
ans+=cost[i];
//ans = cost_counter(N-1);
printf("Case %d: %d\n",Case++,ans);
}
}*/