zoukankan      html  css  js  c++  java
  • hdu 5422 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

    Hint
    You can only add an edge between 1 and 2.
     
    Source
     

    如果连上1-nnn的边,最短距离就是1。所以所有情况下最短距离都是1。

    考虑方案数,如果本来没有1-nnn的边,那么只能连1-nnn,方案数为1。否则怎么连都可以,方案数是n(n−1)2frac{n(n-1)}{2}2n(n1)​​

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<vector>
     5 #include<set>
     6 #include<map>
     7 #include<queue>
     8 #include<algorithm>
     9 using namespace std;
    10 #define N 106
    11 int n,m;
    12 int mp[N][N];
    13 int main()
    14 {
    15     while(scanf("%d%d",&n,&m)==2){
    16             memset(mp,0,sizeof(mp));
    17         for(int i=0;i<m;i++){
    18             int u,v;
    19             scanf("%d%d",&u,&v);
    20             mp[u][v]=mp[v][u]=1;
    21         }
    22         if(mp[1][n]){
    23             printf("1 ");
    24             printf("%d
    ",n*(n-1)/2);
    25         }
    26         else{
    27             printf("1 ");
    28             printf("1
    ");
    29         }
    30     }
    31     return 0;
    32 }
    View Code
  • 相关阅读:
    12 购物车之一(用列表)
    centos7安装pycharm,并创建桌面快捷方式
    centos7安装xrdp
    centos7 安装gitlab
    mysql 新建用户和授权
    django项目连接mysql没有Mysqldb解决办法_使用pymysql代替
    selenium+python实现查询和下载文件
    Python3-使用Requests和正则表达式爬取猫眼Top100的数据
    python3-字典常用操作
    python3-列表常用操作
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4769945.html
Copyright © 2011-2022 走看看