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;
    }
  • 相关阅读:
    SQL Server 动态生成分区脚本
    SQL Server数据库服务器高性能设置
    SQL Server 2005 自动化删除表分区设计方案
    SQL Server 自动化管理分区设计方案(图解)
    简单实用SQL脚本Part:sql多行转为一列的合并问题
    简单实用SQL脚本Part9:纵向回填信息
    SQL Server datetime数据类型设计、优化误区
    SQL Server 创建链接服务器
    SQL Server 数据库最小宕机迁移方案
    SQL Server 表分区注意事项
  • 原文地址:https://www.cnblogs.com/andyqsmart/p/4789415.html
Copyright © 2011-2022 走看看