BestCoder Round #53 (div.2) HDOJ5422 Rikka with Graph(floyd)
来源:互联网 发布:韶关市网络问政 编辑:程序博客网 时间:2024/05/22 19:22
Rikka with Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 232 Accepted Submission(s): 124
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 withn 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
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 numbersn,m(2≤n≤100,0≤m≤100) .
Thenm 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
Then
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 11 2
Sample Output
1 1HintYou can only add an edge between 1 and 2.
如果1-n已经连接了,那么最短路是1,随便连一条边即可,方案数为n * (n - 1) / 2。
如果1-n还没有连接,那么要保证最短只能连接1-n,方案数为1。
AC代码:
#include "iostream"#include "cstdio"#include "cstring"#include "algorithm"using namespace std;const int MAXN = 105;const int INF = 0x3f3f3f3f;int n, m, map[MAXN][MAXN];void floyd(){for(int k = 1; k <= n; ++k)for(int i = 1; i <= n; ++i)for(int j = 1; j <= n; ++j)if(map[i][k] + map[k][j] < map[i][j])map[i][j] = map[i][k] + map[k][j];}int main(int argc, char const *argv[]){while(scanf("%d%d", &n, &m) != EOF) {memset(map, INF, sizeof(map));for(int i = 0; i <= n; ++i)map[i][i] = 1;for(int i = 0; i < m; ++i) {int x, y;scanf("%d%d", &x, &y);map[x][y] = map[y][x] = 1;}floyd();if(map[1][n] == 1) printf("1 %d\n", n * (n - 1) / 2);else printf("1 1\n");}return 0;}
1 0
- BestCoder Round #53 (div.2) HDOJ5422 Rikka with Graph(floyd)
- BestCoder Round #53 (div.2) 1001 Rikka with Graph
- Rikka with Graph-------(BestCoder Round #53 (div.2))
- BestCoder Round #73 (div.2) Rikka with Graph 1002
- BestCoder Round #53 (div.2)1003 Rikka with Graph II 哈密顿路径 dfs
- BestCoder Round #53 (div.2) HDOJ5424 Rikka with Graph II(dfs & 哈密顿通路判断)
- HDU 5422 Rikka with Graph(水题)——BestCoder Round #53(div.2)
- BestCoder Round #53 (div.2) C.Rikka with Graph II(dfs)
- BestCoder Round #53 (div.2) 1002 Rikka with Tree
- BestCoder Round #53 (div.2) HDOJ5423 Rikka with Tree(bfs)
- Rikka with Tree-----(BestCoder Round #53 (div.2))
- BestCoder Round #73 (div.2)-Rikka with Graph(图的连通性)
- BestCoder Round #73 (div.2) Rikka with Chess 1001
- HDU 5631 (BestCoder Round #73 (div.1) 1001)Rikka with Graph(并查集)
- HDU 5423 Rikka with Tree(bfs or dfs)——BestCoder Round #53(div.1 div.2)
- Rikka with string-------(BestCoder Round #37)
- hdu5634 BestCoder Round #73 (div.1) Rikka with Phi 解题报告
- hdu 5203 Rikka with wood sticks(Bestcoder Round #37)
- 关于windows7开机黑屏的解决办法
- 这是我的一条无营养的博客
- 英语学习录——八月
- CListCtrl控件详解(二)
- UML——对象图
- BestCoder Round #53 (div.2) HDOJ5422 Rikka with Graph(floyd)
- 线程池---多线程学习笔记(三)
- 浅谈git
- OSPF的stub区域
- 人类社会——为什么要有秩序
- iPhone开发之动态创建控件(纯代码创建控件)
- 数据结构学习之路-第二章:循环链表
- BackboneJs入门学习[04]—Model实践(2)
- 找出字符串中第一个只出现一次的字符 华为OJ