您的位置:首页 > 其它

hdu 5876 Sparse Graph【最短路+思维】好题

2016-09-10 22:46 316 查看

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 300    Accepted Submission(s): 100


[align=left]Problem Description[/align]
In graph theory, the complement
of a graph G
is a graph H
on the same vertices such that two distinct vertices of
H
are adjacent if and only if they are not
adjacent in G.

Now you are given an undirected graph G
of N
nodes and M
bidirectional edges of unit
length. Consider the complement of G,
i.e., H.
For a given vertex S
on H,
you are required to compute the shortest distances from
S
to all N−1
other vertices.
 

[align=left]Input[/align]
There are multiple test cases. The first line of input is an integer
T(1≤T<35)
denoting the number of test cases. For each test case, the first line contains two integers
N(2≤N≤200000)
and M(0≤M≤20000).
The following M
lines each contains two distinct integers u,v(1≤u,v≤N)
denoting an edge. And S (1≤S≤N)
is given on the last line.
 

[align=left]Output[/align]
For each of T
test cases, print a single line consisting of N−1
space separated integers, denoting shortest distances of the remaining
N−1
vertices from S
(if a vertex cannot be reached from S, output ``-1" (without quotes) instead) in ascending order of vertex number.
 

[align=left]Sample Input[/align]

1
2 0
1

 

[align=left]Sample Output[/align]

1

 

[align=left]Source[/align]
2016 ACM/ICPC Asia Regional Dalian Online 

题目大意:

给你n个点,m条无向边,让你求其无向完全图的补图的单源最短路。

思路:

网赛之后不知道为什么把数据范围m从5500改大了,但是我是按照5500做的,通过了....赛后也是通过的....

1、这算是出题事故了,一开始的时候说是M《=5500,那么问题其实就并不难了,如N大于了6000(大一点稍微稳妥一点),那么狠显然,无论这m条边如何建立的,其补图的从源点到其各个点的最短路要么是1,要么是2,因为很明显,如果m==0的时候,其源点到其他各个点都有一条值为1的最短路径直接相连,而且明显5500条边,当N大于5500的时候,是不可能将从源点到其各个点的最短路变成2以上的情况的(因为假设n==6000的时候,从源点到其他各个点的距离为2的走法一共有6000-2种,明显只去掉5500条边,是不能够使得某些最短距离变成3的)。。

2、那么当n<=6000的时候,直接按照其邻接矩阵跑其补图的最短路即可,否则如果n>6000那么按照输入的边,如果对应输入的无向边中有源点作为这条边的某个端点,那么规定其源点到这条边的另外一个端点的距离为2,即可。

Ac代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int dis[300005];
int vis[20005];
int map[6005][6005];
int bian[10000][3];
int n,m,ss,cont;
void Dij()
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
dis[ss]=0;
for(int i=0;i<n;i++)
{
int u=-1;
int tmp=0x3f3f3f3f;
for(int j=1;j<=n;j++)
{
if(vis[j]==0&&tmp>dis[j])
{
u=j;
tmp=dis[j];
}
}
vis[u]=1;
if(u==-1)continue;
for(int j=1;j<=n;j++)
{
int v=j;
int w=map[u][j];
if(w==0x3f3f3f3f)continue;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
}
}
}
for(int i=1;i<=n;i++)
{
if(dis[i]==0x3f3f3f3f)dis[i]=-1;
}
int f=0;
for(int i=1;i<=n;i++)
{
if(i==ss)continue;
if(f==0)printf("%d",dis[i]);
else printf(" %d",dis[i]);
f++;
}
printf("\n");
}
void Slove()
{
for(int i=0;i<6004;i++)
{
for(int j=0;j<6004;j++)
{
map[i][j]=1;
}
}
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
map[x][y]=0x3f3f3f3f;
map[y][x]=0x3f3f3f3f;
}
scanf("%d",&ss);
Dij();
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
if(n<=6000)
{
Slove();
}
else
{
for(int i=1;i<=n;i++)dis[i]=1;
for(int i=0;i<m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
bian[i][0]=x;
bian[i][1]=y;
}
scanf("%d",&ss);
for(int i=0;i<m;i++)
{
int x=bian[i][0];
int y=bian[i][1];
if(x==ss)
{
dis[y]=2;
}
if(y==ss)
{
dis[x]=2;
}
}
int f=0;
for(int i=1;i<=n;i++)
{
if(i==ss)continue;
if(f==0)
printf("%d",dis[i]);
else printf(" %d",dis[i]);
f++;
}
printf("\n");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu 5876 杭电 5876