forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
|
||
using namespace std; | ||
|
||
#define MAXV 100000 | ||
#define MAXE 10000000 | ||
|
||
int E = 0,to[MAXE],next[MAXE],last[MAXV]; | ||
int Q[MAXV],head,tail,dist[MAXV]; | ||
|
||
void add_edge(int u, int v){ | ||
to[E] = v; next[E] = last[u]; last[u] = E++; | ||
} | ||
|
||
int main(){ | ||
int N; | ||
|
||
scanf("%d",&N); | ||
|
||
memset(last,-1,sizeof last); | ||
|
||
for(int i = 0,u,v,m;i < N;++i){ | ||
scanf("%d %d",&u,&m); | ||
|
||
for(int j = 0;j < m;++j){ | ||
scanf("%d",&v); | ||
add_edge(u,v); | ||
} | ||
} | ||
|
||
head = tail = 0; | ||
memset(dist,-1,sizeof dist); | ||
|
||
int s,t; | ||
scanf("%d %d",&s,&t); | ||
|
||
Q[tail++] = s; | ||
dist[s] = 0; | ||
|
||
while(head < tail){ | ||
int cur = Q[head++]; | ||
|
||
for(int e = last[cur],x;e != -1;e = next[e]){ | ||
x = to[e]; | ||
|
||
if(dist[x] == -1){ | ||
Q[tail++] = x; | ||
dist[x] = 1 + dist[cur]; | ||
} | ||
} | ||
} | ||
|
||
printf("%d %d %d\n",s,t,dist[t] - 1); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
int sz[8],cost[8],L[8][1000]; | ||
int x[1000],y[1000],parent[1001]; | ||
|
||
int Find(int x){ | ||
if(parent[x] != x) parent[x] = Find(parent[x]); | ||
return parent[x]; | ||
} | ||
|
||
void Union(int x, int y){ | ||
x = Find(x); y = Find(y); | ||
parent[x] = y; | ||
} | ||
|
||
struct edge{ | ||
int u,v,w; | ||
|
||
edge(){} | ||
edge(int _u, int _v, int _w) : u(_u), v(_v), w(_w){} | ||
|
||
bool operator < (edge X)const{ | ||
return w < X.w; | ||
} | ||
}e[1000000]; | ||
|
||
int main(){ | ||
int n,m; | ||
|
||
scanf("%d %d",&n,&m); | ||
|
||
for(int i = 0;i < m;++i){ | ||
scanf("%d %d",&sz[i],&cost[i]); | ||
|
||
for(int j = 0;j < sz[i];++j) | ||
scanf("%d",&L[i][j]); | ||
} | ||
|
||
for(int i = 0;i < n;++i) | ||
scanf("%d %d",&x[i],&y[i]); | ||
|
||
int E = 0; | ||
|
||
for(int i = 0;i < n;++i) | ||
for(int j = i + 1;j < n;++j) | ||
e[E++] = edge(i + 1,j + 1,(x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j])); | ||
|
||
sort(e,e + E); | ||
|
||
long long ans = (1LL << 60); | ||
|
||
for(int mask = 0;mask < (1 << m);++mask){ | ||
long long aux = 0; | ||
|
||
for(int i = 1;i <= n;++i) | ||
parent[i] = i; | ||
|
||
for(int i = 0;i < m;++i){ | ||
if(mask & (1 << i)){ | ||
aux += cost[i]; | ||
|
||
for(int j = sz[i] - 1;j > 0;--j) | ||
Union(L[i][j],L[i][0]); | ||
} | ||
} | ||
|
||
for(int i = 0;i < E;++i){ | ||
if(Find(e[i].u) != Find(e[i].v)){ | ||
aux += e[i].w; | ||
Union(e[i].u,e[i].v); | ||
} | ||
} | ||
|
||
ans = min(ans,aux); | ||
} | ||
|
||
printf("%d\n",ans); | ||
|
||
return 0; | ||
} |