hdu5422 Rikka with Graph(图论简单规律题)

173 篇文章 3 订阅
171 篇文章 0 订阅


Link:http://acm.hdu.edu.cn/showproblem.php?pid=5422

Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 163    Accepted Submission(s): 83


Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Yuta has a non-direct graph with  n  vertices and  m  edges. The length of each edge is 1. Now he wants to add exactly an edge which connects two different vertices and minimize the length of the shortest path between vertice 1 and vertice  n . Now he wants to know the minimal length of the shortest path and the number of the ways of adding this edge.

It is too difficult for Rikka. Can you help her?
 

Input
There are no more than 100 testcases. 

For each testcase, the first line contains two numbers  n,m(2n100,0m100) .

Then  m  lines follow. Each line contains two numbers  u,v(1u,vn)  , which means there is an edge between  u  and  v . There may be multiedges and self loops.
 

Output
For each testcase, print a single line contains two numbers: The length of the shortest path between vertice 1 and vertice  n  and the number of the ways of adding this edge.
 

Sample Input
  
  
2 1 1 2
 

Sample Output
  
  
1 1
Hint
You can only add an edge between 1 and 2.
 

Source
 


中文题意:

Rikka with Graph

 
 Accepts: 353
 
 Submissions: 1174
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:

勇太有一张nn个点mm条边的无向图,每一条边的长度都是1。现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短。现在他想要知道最短路最短是多少,以及再保证最短路最短的情况下,他有多少种连边的方案。

当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
输入描述
数据组数不超过100组。每组数据的第一行两个整数n,m(2 \leq n \leq 100, 0 \leq m \leq 100)n,m(2n100,0m100)。

接下来mm行。每行两个整数u,v(1 \leq u,v \leq n)u,v(1u,vn),代表原图中的一条无向边。注意可能有自环和重边。
输出描述
对于每一组数据输出一行两个整数:最短路最短是多少以及加边的方案数。
输入样例
2 1
1 2
输出样例
1 1
Hint
你只能连上1 2这条边。


BC官方题解:

Rikka with Graph

如果连上1-nn的边,最短距离就是1。所以所有情况下最短距离都是1。

考虑方案数,如果本来没有1-nn的边,那么只能连1-nn,方案数为1。否则怎么连都可以,方案数是\frac{n(n-1)}{2}2n(n1)


AC code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define MAXN 1000010
using namespace std;
const int INF=0xffffff;
const int mod=1e9+7; 
LL n,m,ans1,ans2,cnt;
int ma[111][111];
int main()
{
    int i,fg,u,v;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        fg=0;
        cnt=0;
        memset(ma,0,sizeof(ma));
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&u,&v);
            if((u==1&&v==n)||(u==n&&v==1))
                fg=1;
            if(u!=v&&ma[u][v]==0)
            {
                ma[u][v]=1;
                ma[v][u]=1;
                cnt++;
            }
        }
        if(!fg)
        {
            printf("1 1\n");
        }
        else
        {
            printf("1 %d\n",n*(n-1)/2);
        }
    }
    return 0;
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值