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;
    }
  • 相关阅读:
    爬虫必备的web知识
    pythoth 中常用的魔法方法
    Python数据分析matplotlib可视化之绘图
    前端(HTML)+后端(Django)+数据库(MySQL):用户注册及登录演示
    用python文件操作实现复制图片、视频
    彩票购买系统(26选5)-初级版本
    封装系统字符串内置函数,实现类似功能
    部署live555到云
    腾讯云:基于 Ubuntu 搭建 VNC 远程桌面服务
    2.Linux文件IO编程
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4789415.html
Copyright © 2011-2022 走看看