POJ3170+2395+2376+2229(练习二)

来源:互联网 发布:红色骑兵军 知乎 编辑:程序博客网 时间:2024/06/03 09:17
POJ3170
Bessie is in Camelot and has encountered a sticky situation: she needs to pass through the forest that is guarded by the Knights of Ni. In order to pass through safely, the Knights have demanded that she bring them a single shrubbery. Time is of the essence, and Bessie must find and bring them a shrubbery as quickly as possible.

Bessie has a map of of the forest, which is partitioned into a square grid arrayed in the usual manner, with axes parallel to the X and Y axes. The map is W x H units in size (1 <= W <= 1000; 1 <= H <= 1000). 

The map shows where Bessie starts her quest, the single square where the Knights of Ni are, and the locations of all the shrubberies of the land. It also shows which areas of the map can be traverse (some grid blocks are impassable because of swamps, cliffs, and killer rabbits). Bessie can not pass through the Knights of Ni square without a shrubbery. 

In order to make sure that she follows the map correctly, Bessie can only move in four directions: North, East, South, or West (i.e., NOT diagonally). She requires one day to complete a traversal from one grid block to a neighboring grid block. 

It is guaranteed that Bessie will be able to obtain a shrubbery and then deliver it to the Knights of Ni. Determine the quickest way for her to do so.
Input
Line 1: Two space-separated integers: W and H. 

Lines 2..?: These lines describe the map, row by row. The first line describes the most northwest part of the map; the last line describes the most southeast part of the map. Successive integers in the input describe columns of the map from west to east. Each new row of a map's description starts on a new input line, and each input line contains no more than 40 space-separated integers. If W <= 40, then each input line describes a complete row of the map. If W > 40, then more than one line is used to describe a single row, 40 integers on each line except potentially the last one. No input line ever describes elements of more than one row. 

The integers that describe the map come from this set: 
0: Square through which Bessie can travel 
1: Impassable square that Bessie cannot traverse 
2: Bessie's starting location 
3: Location of the Knights of Ni 
4: Location of a shrubbery
Output
Line 1: D, the minimum number of days it will take Bessie to reach a shrubbery and bring it to the Knights of Ni.
Sample Input
8 44 1 0 0 0 0 1 00 0 0 1 0 1 0 00 2 1 1 3 0 4 00 0 0 4 1 1 1 0
Sample Output
11
题意:给出一张地图 起点2 重点3 障碍1 可行0 
在bfs最短路的问题基础上加一个条件 就是2到3的过程中经过至少一次4
思路:从终点和起点分别bfs  求出到各个点的距离
记录下4的位置  到最后取最小值
  • Source Code
    #include<iostream>#include<stdio.h>#include<string.h>#include<cmath>#include<vector>#include<queue>using namespace std;int w,h;int dx[]={1,-1,0,0};int dy[]={0,0,-1,1};struct node{    int x;    int y;}b[1001];int d[2][1001][1001];int a[1001][1001];int sx,sy,ex,ey;vector<node>v;queue<node>q;int vis[1001][1001];int judge(int x,int y){    if(x>0&&x<=h&&y>0&&y<=w)    return 1;    else    return 0;}void bfs(int x,int y,int flag){    int i,j;    node now,next;    while(!q.empty())    q.pop();    now.x=x;    now.y=y;    vis[x][y]=1;    q.push(now);    d[flag][x][y]=0;    while(!q.empty())    {        now=q.front();        q.pop();        for(i=0;i<4;i++)        {            int xx=now.x+dx[i];            int yy=now.y+dy[i];            if (!judge(xx,yy))            continue;            if (vis[xx][yy]||a[xx][yy]==1)            continue;            if (d[flag][xx][yy]>d[flag][now.x][now.y]+1)            {                d[flag][xx][yy] = d[flag][now.x][now.y]+1;                if (!vis[xx][yy])                {                    next.x=xx;                    next.y=yy;                    vis[xx][yy]=1;                    q.push(next);                    }                }            }        }    }int main(){    int i,j;    //int w,h;    while(cin>>w>>h)    {        memset(d,0x3f3f3f3f,sizeof(d));        int num=0;        for(i=1;i<=h;i++)        {            for(j=1;j<=w;j++)            {                scanf("%d",&a[i][j]);                if(a[i][j]==2)sx=i,sy=j;                if (a[i][j]==3)ex=i,ey=j;                if (a[i][j]==4)                {                    b[num].x=i;                    b[num].y=j;                    num++;                    }                }            }        memset(vis,0,sizeof(vis));        bfs(sx,sy,0);        memset(vis,0,sizeof(vis));        bfs(ex,ey,1);        int len=v.size();        int ans=1e+6;        for(i=0;i<num;i++)        {            ans=min(ans,d[0][b[i].x][b[i].y]+d[1][b[i].x][b[i].y]);            }        cout<<ans<<endl;        }    return 0;    }

POJ2395
The cows have run out of hay, a horrible event that must be remedied immediately. Bessie intends to visit the other farms to survey their hay situation. There are N (2 <= N <= 2,000) farms (numbered 1..N); Bessie starts at Farm 1. She'll traverse some or all of the M (1 <= M <= 10,000) two-way roads whose length does not exceed 1,000,000,000 that connect the farms. Some farms may be multiply connected with different length roads. All farms are connected one way or another to Farm 1. Bessie is trying to decide how large a waterskin she will need. She knows that she needs one ounce of water for each unit of length of a road. Since she can get more water at each farm, she's only concerned about the length of the longest road. Of course, she plans her route between farms such that she minimizes the amount of water she must carry. Help Bessie know the largest amount of water she will ever have to carry: what is the length of longest road she'll have to travel between any two farms, presuming she chooses routes that minimize that number? This means, of course, that she might backtrack over a road in order to minimize the length of the longest road she'll have to traverse.
Input
* Line 1: Two space-separated integers, N and M. * Lines 2..1+M: Line i+1 contains three space-separated integers, A_i, B_i, and L_i, describing a road from A_i to B_i of length L_i.
Output
* Line 1: A single integer that is the length of the longest road required to be traversed.
Sample Input
3 31 2 232 3 10001 3 43
Sample Output
43
Hint
OUTPUT DETAILS: 

In order to reach farm 2, Bessie travels along a road of length 23. To reach farm 3, Bessie travels along a road of length 43. With capacity 43, she can travel along these roads provided that she refills her tank to maximum capacity before she starts down a road.
题意:求最小生成树的最大边
思路
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;int map[2001][2001];int vis[2001];int d[2001];int n,m;int prim(){    int i,j,k;    k=1;    for(i=1;i<=n;i++)    {        d[i]=map[1][i];        }    vis[1]=1;    int max=0;    for(i=1;i<n;i++)    {        int min=1e+10;        int flag=0;        for(j=1;j<=n;j++)        {            if(!vis[j]&&d[j]<min)            {                min=d[j];                flag=1;                k=j;                }            }        if(!flag)            break;        if(max<min)            max=min;        vis[k]=1;        for(j=1;j<=n;j++)            if(!vis[j]&&d[j]>map[k][j])                d[j]=map[k][j];        }    return max;    }int main(){    int i,j,k;    int a,b,c;    while(cin>>n>>m)    {        memset(vis,0,sizeof(vis));        for(i=1;i<=n;i++)            for(j=1;j<=n;j++)            {                if(i!=j)                map[i][j]=1e+10;                else                map[i][j]=0;                }        for(i=0;i<m;i++)        {            cin>>a>>b>>c;            if(map[a][b]>c)            {                map[a][b]=c;                map[b][a]=c;                }            }        cout<<prim()<<endl;        }    return 0;    }
POJ2376
Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cleaning chores around the barn. He always wants to have one cow working on cleaning things up and has divided the day into T shifts (1 <= T <= 1,000,000), the first being shift 1 and the last being shift T. Each cow is only available at some interval of times during the day for work on cleaning. Any cow that is selected for cleaning duty will work for the entirety of her interval. Your job is to help Farmer John assign some cows to shifts so that (i) every shift has at least one cow assigned to it, and (ii) as few cows as possible are involved in cleaning. If it is not possible to assign a cow to each shift, print -1.
Input
* Line 1: Two space-separated integers: N and T * Lines 2..N+1: Each line contains the start and end times of the interval during which a cow can work. A cow starts work at the start time and finishes after the end time.
Output
* Line 1: The minimum number of cows Farmer John needs to hire or -1 if it is not possible to assign a cow to each shift.
Sample Input
3 101 73 66 10
Sample Output
2
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

INPUT DETAILS: 

There are 3 cows and 10 shifts. Cow #1 can work shifts 1..7, cow #2 can work shifts 3..6, and cow #3 can work shifts 6..10. 

OUTPUT DETAILS: 

By selecting cows #1 and #3, all shifts are covered. There is no way to cover all the shifts using fewer than 2 cows.

题意:每只牛都有工作的时间 给出开始结束的时间 问你最少几头牛可以满足每个小时内都至少有一头牛在工作 也就是用小区间覆盖满这个大区间
思路:贪心
与看最多电视节目有点类似 先根据开始的时间排下序
然后当前的区间尾部确定 每一次都选择则下一个开始时间在尾部时间内并且结束时间最大的
扫一遍 结尾部分 自后一头牛的结束时间如果不满足覆盖 则-1

  • Source Code
    #include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct cow{    int st;    int end;    }a[1000001];bool cmp(cow a, cow b){    return a.st<b.st;    }int main(){    int n,m;    int i,j;    while(cin>>n>>m)    {        memset(a,0x3f3f3f,sizeof(a));        for(i=1;i<=n;i++)        scanf("%d %d",&a[i].st,&a[i].end);        sort(a+1,a+n+1,cmp);        int t=0,temp=0,ans=0;        bool flag=0;        for(i=1;i<=n;i++)        {            if (a[i].st<=t+1)            {                if (temp<a[i].end)                {                    temp=a[i].end;                    flag=1;                    }                if (a[i+1].st>t+1&&flag)                {                    t=temp;                    ++ans;                    flag=0;                    }                }            }        if(t<m)cout<<-1<<endl;        else cout<<ans<<endl;    }    return 0;}
POJ2229
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an integer power of 2. Here are the possible sets of numbers that sum to 7: 

1) 1+1+1+1+1+1+1 
2) 1+1+1+1+1+2 
3) 1+1+1+2+2 
4) 1+1+1+4 
5) 1+2+2+2 
6) 1+2+4 

Help FJ count all possible representations for a given integer N (1 <= N <= 1,000,000). 
Input
A single line with a single integer, N.
Output
The number of ways to represent N as the indicated sum. Due to the potential huge size of this number, print only last 9 digits (in base 10 representation).
Sample Input
7
Sample Output
6
题意:求n可以由多少种方式累加和得到 只能是2的倍数包括1
思路:乍一看没啥思路
写几个之后就会发现
很好理解 是奇数的时候 在前一项式子的基础上+1 只有这一种 即a[n]=a[m-1]
偶数的时候复杂一点 6 为例
1+1+1+1+1+1+1
1+1+1+1+2
1+1+2+2
1+1+4
2+2+2
2+4
前面部分 去掉1 就是a[5]的组合
后面的将灭个数除以2 1+1+1 1+2 很容易看出a[3]的组合
  • Source Code
    #include<iostream>#include<stdio.h>using namespace std;long long ans[1000001];int main(){    int i,j;    ans[1]=1;    ans[2]=2;    for(i=3;i<1000001;i++)    {        if(i%2==0)        ans[i]=ans[i-2]+ans[i/2];        else ans[i]=ans[i-1];        if(ans[i]>=1000000000)        ans[i]-=1000000000;        }    int n;    while(cin>>n)    {        cout<<ans[n]<<endl;        }    return 0;    }