zoukankan      html  css  js  c++  java
  • 【比赛练习ac题】poj2253+hdu2717+poj2387+poj1258【解题报告】

    A - Catch That Cow

    Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting.

    * Walking: FJ can move from any point X to the points - 1 or + 1 in a single minute
    * Teleporting: FJ can move from any point X to the point 2 × X in a single minute.

    If the cow, unaware of its pursuit, does not move at all, how long does it take for Farmer John to retrieve it?

    Input

    Line 1: Two space-separated integers: N and K

    Output

    Line 1: The least amount of time, in minutes, it takes for Farmer John to catch the fugitive cow.

    Sample Input

    5 17

    Sample Output

    4

    Hint

    The fastest way for Farmer John to reach the fugitive cow is to move along the following path: 5-10-9-18-17, which takes 4 minutes.
     
    题意:输入起点和终点,问从起点到终点的最少时间,有三种走法,x+1或者x-1或者x*2.
    思路:BFS模板题,注意数组不能越界
    #include<stdio.h>
    #include<iostream>
    #include<queue>
    #include<string.h>
    using namespace std;
    #define N 300010
    int book[2*N];
    int start1,end,sum;
    
    struct node{
        int x,step;
    };
    void BFS()
    {
        node now,start,next1,next2,next3;
        queue<node>Q;
        start.step = 0;
        start.x = start1;
        Q.push(start);
        book[start1]  =1;
        while(!Q.empty())
        {
            now = Q.front() ;
            Q.pop();
            if(now.x == end)
            {//printf("%d
    ",now.x );
                sum = now.step ;
                return;
            }
            next1.x = now.x + 1;
            if(next1.x < N&&!book[next1.x])
            {
                next1.step = now.step + 1;
                book[next1.x] = 1;
                Q.push(next1); 
            }
            next2.x = now.x -1;
            if(next2.x >= 0&&!book[next2.x])
            {
                next2.step = now.step + 1;
                book[next2.x ] = 1;
                Q.push(next2); 
            }
            next3.x = now.x *2;
            if(next3.x >= 0&&next3.x < N&&!book[next3.x])
            {
                next3.step = now.step  +1;
                book[next3.x] = 1;
                Q.push(next3); 
            }
         } 
        return;
    }
    
    int main()
    {
        while(scanf("%d%d",&start1,&end)!=EOF)
        {
            memset(book,0,sizeof(book));
            BFS();
            printf("%d
    ",sum);
        }
        return 0;
    }

    B - Til the Cows Come Home

     
    Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible. 

    Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it. 

    Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
    Input
    * Line 1: Two integers: T and N 

    * Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
    Output
    * Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
    Sample Input
    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100
    Sample Output
    90
    Hint
    INPUT DETAILS: 

    There are five landmarks. 

    OUTPUT DETAILS: 

    Bessie can get home by following trails 4, 3, 2, and 1.
    题意:输入顶点数和路径数,每行输入起点和终点及边权,问,从n到1的最短路径是多少
    思路:dijkstra,双向存图
    #include<stdio.h>
    #include<string.h>
    #define N 2300
    #define inf 99999999
    int e[N][N],book[N],dis[N];
    int main()
    {
        int t,n,t1,t2,t3,min,i,j,u;
        while(scanf("%d%d",&t,&n)!=EOF)
        {
            for( i = 1; i <= n; i ++)
                for(int j = 1; j <= n; j ++)
                    if(i ==j)
                        e[i][j] = 0;
                    else
                        e[i][j] = inf;
            memset(book,0,sizeof(book));
            for( i= 1; i <= t; i ++)
            {
                scanf("%d%d%d",&t1,&t2,&t3);
                if(t3 <e[t1][t2])
                {
                    e[t1][t2] = t3;
                    e[t2][t1] = t3;
                }
                    
            }
            for( i = 1; i <= n; i ++)
                dis[i] = e[1][i]; 
            book[1] = 1;    
            for( i = 1; i <= n-1; i ++)
            {
                min  = inf;
                for( j = 1; j <= n; j ++)
                {
                    if(!book[j]&&dis[j]<min)
                    {
                         u = j;
                        min = dis[j];
                    }
                }
                book[u] = 1;
                for( j = 1; j <= n; j ++)
                {  
                    if(e[u][j]<inf)
                    if(!book[j]&&dis[j] > dis[u]+e[u][j])
                        dis[j] = dis[u]+e[u][j];
                }
            }
            
            printf("%d
    ",dis[n]);            
        }
        return 0;
    }

    D - Agri-Net

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
    The distance between any two farms will not exceed 100,000. 

    Input

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    Sample Output

    28

    题意:输入矩阵,连通每个点的最小权值。
    思路:prime算法。
    #include<stdio.h>
    #include<string.h>
    
    #define N 1010
    #define inf 99999999
    
    int dis[N], book[N], n, e[N][N];
    
    int main()
    {
        int i,  s, sum, t, x, y, z, Min, j;
        while(~scanf("%d", &n)){
            memset(book, 0, sizeof(book));
            for(i = 1; i <= n; i++){
                for(j = 1; j <= n; j++){
                    scanf("%d", &e[i][j]);
                }
            }
            for(i = 1; i <= n; i++)
                dis[i] = e[1][i];
            book[1] = s = 1;
            sum = 0;
            while(s < n){
                Min = inf;
                for(i = 1; i <= n; i++)
                    if(!book[i] && dis[i] < Min)
                        Min = dis[i], j = i;
                s++;
                book[j] = 1;
                sum += dis[j];
                for(i = 1; i <= n;  i++){
                    if(!book[i] && dis[i]>e[j][i])
                        dis[i] = e[j][i];
                }
            }
            printf("%d
    ", sum);
        }
        return 0;
    }

    I - Frogger

    Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' sunscreen, he wants to avoid swimming and instead reach her by jumping. 
    Unfortunately Fiona's stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps. 
    To execute a given sequence of jumps, a frog's jump range obviously must be at least as long as the longest jump occuring in the sequence. 
    The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones. 

    You are given the coordinates of Freddy's stone, Fiona's stone and all other stones in the lake. Your job is to compute the frog distance between Freddy's and Fiona's stone. 

    Input

    The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy's stone, stone #2 is Fiona's stone, the other n-2 stones are unoccupied. There's a blank line following each test case. Input is terminated by a value of zero (0) for n.

    Output

    For each test case, print a line saying "Scenario #x" and a line saying "Frog Distance = y" where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.

    Sample Input

    2
    0 0
    3 4
    
    3
    17 4
    19 4
    18 5
    
    0
    

    Sample Output

    Scenario #1
    Frog Distance = 5.000
    
    Scenario #2
    Frog Distance = 1.414
    题意:先输入起点和终点的坐标,再输入n-2个坐标,输出从1到n,最大边权的最小值(找到每条路径的最大值,再从这些路径中找到最小值)
    注意输出保留三位小数,每个样例后输出一个空行。
    思路:dijktra的变形题。每次比较出起点到当前路径的dis[u]值和顶点u到当前路径的值的e[u][j]的较大那个值,再判断当前路径的dis[j]
    是否大于较大值,若大于则更新。
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #define inf 99999999
    #define N 250
    int book[N];
    int n,t,i,j,u;
    double min;
    double x,e[N][N],dis[N];
    struct node{
        double x,y;
    };
    
    node w[N];
    
    int main()
    {
        t = 0;
        while(scanf("%d",&n),n!=0)
        {
            t ++;
            scanf("%lf%lf",&w[1].x ,&w[1].y);
            scanf("%lf%lf",&w[n].x ,&w[n].y);
            for(i = 2; i <= n-1; i ++)
                scanf("%lf%lf",&w[i].x ,&w[i].y );
            for(i = 1; i <= n; i ++)
                for(j = 1; j <= n; j ++)
                    if(i == j)
                        e[i][j] = 0;
                    else
                        e[i][j] = sqrt((w[i].x -w[j].x)*(w[i].x -w[j].x)+(w[i].y -w[j].y)*(w[i].y -w[j].y));
            memset(book,0,sizeof(book));
            for(i = 1; i <= n; i ++)
            {
                dis[i] = e[1][i];
            }
                
            for(i = 1; i <= n-1; i ++)
            {
                min = inf;
                for(j = 1; j <= n; j ++)
                {
                    if(!book[j]&&dis[j] < min)
                    {
                        min = dis[j];
                        u = j;
                    }
                 } 
                 book[u] = 1;
                 for(j = 1; j <= n;j ++)
                 {
                     if(e[u][j] > dis[u])
                         x = e[u][j];
                     else
                         x = dis[u];
                     if(!book[j]&&dis[j] > x)
                         dis[j] =x;
                 }
            }
            printf("Scenario #%d
    ",t);
            printf("Frog Distance = %.3lf
    
    ",dis[n]);
        }
        return 0;
    }
  • 相关阅读:
    redhat安装opencv
    vsftpd的配置与使用
    Redhat 安装编译 Python-2.7.12
    YUM 安装与配置
    docker安装mysql
    高频问题 java8新特性(转载)
    quartz简单实例实现
    java8线程池
    java8多线程不带返回值
    java8多线程带返回值的
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7381835.html
Copyright © 2011-2022 走看看