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;
    }
  • 相关阅读:
    MySQL客户端管理
    Windows10安装Pytorch环境要点
    使用ssh加密github通信
    JVM 对象状态判断01
    并发之AbstractQueuedLongSynchronize----AQS
    关于CountDownLatch控制线程的执行顺序
    关于线程执行顺序的问题
    并发之Striped64(l累加器)
    并发之线程以及线程的中断状态
    1 JPA入门----项目搭建以及CRUD
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4789415.html
Copyright © 2011-2022 走看看