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
  • 相关阅读:
    C# 验证IP地址、Email格式、URl网址
    如何创建、安装和调试Windows服务
    C#发送Email邮件方法总结
    C#调用java类、jar包方法。
    未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序
    在已经存在的表上创建索引
    Windows下的.NET+ Memcached安装
    把表从Access2007导出到Sql Server
    FusionCharts参数说明
    Sublime Text 3 之配置package control
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4769945.html
Copyright © 2011-2022 走看看