题目链接: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): 657 Accepted Submission(s): 327
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(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
Sample Output
1 1
Hint
You can only add an edge between 1 and 2.
Source
BestCoder Round #53 (div.2)
Recommend
hujie | We have carefully selected several similar problems for you: 5503 5502 5501 5499 5498
题目大意:给一个图,每一个边长都为1。然后图中给你一下边,你需要加一条边使1到n距离最短。
要求输出最短路径和连线的方法数。
题目思路:这题很容易看出最短路一定是将1到n连一条线,最短路径一定为1。对于方法数:其中如果1到n之间已经有线的话,就要从n个点中随机挑出2个点连线。为n*(n-1)/2种;如果1到n有线的话直接输出1就好了。
详见代码。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n,m;
int Map[110][110];
while (~scanf("%d%d",&n,&m))
{
int u,v;
memset(Map,0,sizeof(Map));
for (int i=1; i<=m; i++)
{
scanf("%d%d",&u,&v);
Map[u][v]=Map[v][u]=1;
}
if (Map[1][n]==1)
{
int ans=n*(n-1)/2;
printf ("1 %d\n",ans);
}
else
{
printf ("1 1\n");
}
}
return 0;
}