HDU 5422 Rikka with Graph(水题)

Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)

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 withvertices andedges. 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. 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.Thenlines follow. Each line contains two numbers, which means there is an edge between. 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 verticeand the number of the ways of adding this edge.

Sample Input

2 11 2

Sample Output

1 1

Hint

You can only add an edge between 1 and 2.

Source

/************************************************************************/

附上该题对应的中文题

Rikka with Graph

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

问题描述

众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:勇太有一张个点条边的无向图,每一条边的长度都是1。现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到号点的最短路尽可能的短。现在他想要知道最短路最短是多少,以及再保证最短路最短的情况下,,他有多少种连边的方案。当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?

输入描述

数据组数不超过100组。每组数据的第一行两个整数。接下来行。每行两个整数,代表原图中的一条无向边。注意可能有自环和重边。

输出描述

对于每一组数据输出一行两个整数:最短路最短是多少以及加边的方案数。

输入样例

2 11 2

输出样例

1 1

Hint

你只能连上1 2这条边。

出题人的解题思路:

Rikka with Graph

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

考虑方案数,如果本来没有1-的边,那么只能连1-,方案数为1。否则怎么连都可以,方案数是。

该题算是比较简单的一道题,因为每条边的长度固定是1,那么结点1到结点n的最短路必定是从结点1直达结点n,即最短路为1

那么,情况无非两种:

①本身没有结点1到结点n的边,那么我们只有一种方案可以使最短路为1,即添上一条结点1到结点n的边

②本身已经存在结点1到结点n的边,也就是说已经存在最短路,那么我们便可以添加任意两个结点之间的边,这样的话,按照完全图的定义,方案数为n*(n-1)/2

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<stdio.h>#include<string.h>#include<stdlib.h>#include<queue>#include<stack>#include<math.h>#include<vector>#include<map>#include<set>#include<stdlib.h>#include<cmath>#include<string>#include<algorithm>#include<iostream>#define exp 1e-10using namespace std;const int N = 7061;const int inf = 2147483647;const int mod = 2009;int main(){int n,m,i,a,b;bool flag;while(~scanf("%d%d",&n,&m)){flag=false;for(i=0;i<m;i++){scanf("%d%d",&a,&b);if(a==1&&b==n||a==n&&b==1)flag=true;}if(flag)printf("1 %d\n",n*(n-1)/2);elseputs("1 1");}return 0;}菜鸟成长记

版权声明:本文为博主原创文章,未经博主允许不得转载。

那绿叶上的水珠,是思念的泪滴。

HDU 5422 Rikka with Graph(水题)

相关文章:

你感兴趣的文章:

标签云: