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;
    }
  • 相关阅读:
    文件权限---I
    python查看变量在内存中的地址
    Python3 基本数据类型
    print语法
    任务管理器启动项显示“没有可显示的启动项”如何解决?
    nginx 虚拟主机配置
    nginx 高级应用
    nginx 日志文件详解
    nginx 编译安装与配置使用
    python获取header脚本
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4789415.html
Copyright © 2011-2022 走看看