• 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
  • 相关阅读:
    元素的高度自适应
    关于IE6的一些常见的CSS BUG处理
    Vue项目在IE浏览器报错polyfilleventsource added missing EventSource to window
    Springboot使用JdbcTemplate RowMapper查询,直接返回实体列表
    Springboot启动工程后,浏览器出现输入用户名和密码
    mysql5.6 zip版本如何安装
    python基础基础知识介绍
    python基础数据类型,集合及深浅copy
    格式化输出
    python基础windows环境下 安装python2和python3
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4769945.html
Copyright © 2020-2023  润新知