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?
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(2≤n≤100,0≤m≤100) .
Then m lines follow. Each line contains two numbers u,v(1≤u,v≤n) , which means there is an edge between u and v . There may be multiedges and self loops.
For each testcase, the first line contains two numbers n,m(2≤n≤100,0≤m≤100) .
Then m lines follow. Each line contains two numbers u,v(1≤u,v≤n) , 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 1HintYou 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)
问题描述
众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的: 勇太有一张n个点m条边的无向图,每一条边的长度都是1。现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到n号点的最短路尽可能的短。现在他想要知道最短路最短是多少,以及再保证最短路最短的情况下,他有多少种连边的方案。 当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
输入描述
数据组数不超过100组。每组数据的第一行两个整数n,m(2≤n≤100,0≤m≤100)。 接下来m行。每行两个整数u,v(1≤u,v≤n),代表原图中的一条无向边。注意可能有自环和重边。
输出描述
对于每一组数据输出一行两个整数:最短路最短是多少以及加边的方案数。
输入样例
2 1 1 2
输出样例
1 1
Hint
你只能连上1 2这条边。
Rikka with Graph
如果连上1-n的边,最短距离就是1。所以所有情况下最短距离都是1。
考虑方案数,如果本来没有1-n的边,那么只能连1-n,方案数为1。否则怎么连都可以,方案数是2n(n−1)。
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;
}