标签:
***Rikka with Graph***
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
2 1
1 2
Sample Output
1 1
题目大意:众所周知,萌萌哒六花不擅长数学,所以勇太给了她一些数学问题做练习,其中有一道是这样的:
勇太有一张nn个点mm条边的无向图,每一条边的长度都是1。现在他想再在这张图上连上一条连接两个不同顶点边,使得1号点到nn号点的最短路尽可能的短。现在他想要知道最短路最短是多少,以及再保证最短路最短的情况下,他有多少种连边的方案。
当然,这个问题对于萌萌哒六花来说实在是太难了,你可以帮帮她吗?
解题思路:
考虑最短距离:很显然,所有情况下最短距离都是1。
考虑方案数:如果本来没有1-n的边,那么只能连1-n,方案数为1;
否则怎么连都可以,方案数是(n-1)*n/2;
上代码:
/*
2015 - 8 - 29 晚上
Author: ITAK
今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int Max = 0xfffffff;
const int maxn = 110;
int dis[maxn][maxn];
int m, n, ans;
//n代表结点个数
//m代表有几条边
int main()
{
while(cin>>n>>m)
{
memset(dis, 0, sizeof(dis));
int u, v, w;
while(m--)
{
cin>>u>>v;
dis[u][v] = 1;
dis[v][u] = 1;
}
int ret;
if(dis[1][n] == 1)
ret = n*(n-1)/2;
else
ret = 1;
cout<<1<<" "<<ret<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/qingshui23/article/details/48092045