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

Sparse Graph

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 300    Accepted Submission(s): 100


Problem Description
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 N1 other vertices.
 

Input
There are multiple test cases. The first line of input is an integer T(1T<35) denoting the number of test cases. For each test case, the first line contains two integers N(2N200000) and M(0M20000) . The following M lines each contains two distinct integers u,v(1u,vN) denoting an edge. And S (1SN) is given on the last line.
 

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

Sample Input
  
  
1 2 0 1
 

Sample Output
  
  
1
 

Source

题目大意:

给你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");
        }
    }
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值