zoukankan      html  css  js  c++  java
  • ACM学习历程—HDU5422 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(2n100,0m100).

    Then m lines follow. Each line contains two numbers u,v(1u,vn) , 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.

    首先最小距离肯定是1,不可能比1还小。

    其次最小距离肯定是1,因为只要连1和n就能达到1。

    那么如果一开始1和n没有相连,那么连接1和n能达到1,否则均大于1。

    如果1和n一开始就相连,那么随便连两条就OK,C(n, 2) = n*(n-1)/2。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <set>
    #include <queue>
    #include <vector>
    #define LL long long
    
    using namespace std;
    
    int n, m;
    bool dis[105][105];
    
    int input()
    {
        if (scanf("%d%d", &n, &m) == EOF)
            return false;
        memset(dis, false, sizeof(dis));
        int u, v;
        for (int i = 0; i < m; ++i)
        {
            scanf("%d%d", &u, &v);
            dis[u][v] = dis[v][u] = true;
        }
        return true;
    }
    
    void work()
    {
        printf("1 ");
        if (dis[1][n])
            printf("%d
    ", n*(n-1)/2);
        else
            printf("1
    ");
    }
    
    int main()
    {
        //freopen("test.in", "r", stdin);
        while (input())
        {
            work();
        }
        return 0;
    }
  • 相关阅读:
    python 输入年月日,返回当天是星期几
    python isdigit()函数
    python 字典复制(存疑)
    python 文本文件操作
    python 字符串实例:检查并判断密码字符串的安全强度
    python isinstance()判断数据类型
    python 字符串方法
    python format使用方法
    python 关于异常处理 try...except... 的两个案例
    gparted 不能起作用的时候,用fdisk
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4789415.html
Copyright © 2011-2022 走看看