HDU 5422 Rikka with Graph
Rikka with Graph
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 182 Accepted Submission(s): 95
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
vertices and
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
. 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
vertices and
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
. 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
.
Then
lines follow. Each line contains two numbers
, which means there is an edge between
and
. There may be multiedges and self loops.
For each testcase, the first line contains two numbers
.
Then
lines follow. Each line contains two numbers
, which means there is an edge between
and
. 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
and the number of the ways of adding this edge.
and the number of the ways of adding this edge.
Sample Input
2 1
1 2
Sample Output
1 1
这道题看似是一个图论题, 然而仔细一看就水的不行啦。 如果 1 和点 n 没有直接相连的路径, 那么最短路径就是这一条。 否则,最短路径还是这一条。 但是连线的方案确是任意乱连都可以。有 n*(n-1)/2个连法。
当时对题意理解错啦: 理解为添加一条路径使1到其他点路径的总和最小。 (呜呜呜呜~~~~)
#include<iostream> #include<cstdio> using namespace std; int main() { int m, n; while(~scanf("%d%d", &n, &m)) { int u, v; int ok = 0; for(int i=0; i<m; i++) { scanf("%d%d", &u, &v); if((u==1&&v==n)||(v==1&&u==n)) ok = 1; } if(!ok) printf("%d %d\n",1, 1); else printf("%d %d\n",1, n*(n-1)/2); } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· C# 13 中的新增功能实操
· 万字长文详解Text-to-SQL
· Ollama本地部署大模型总结
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(4)
· 卧槽!C 语言宏定义原来可以玩出这些花样?高手必看!