2019.5.11 Cross The Threshold Play 上

前六道为各个算法的入门题,后面五道为CF的水题,那天做完到现在还困
学长大佬们速度太快了555
比赛地址:https://cn.vjudge.net/contest/301016#problem

A
HDU - 1272 小希的迷宫

上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走。但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了房间A和B,那么既可以通过它从房间A走到房间B,也可以通过它从房间B走到房间A,为了提高难度,小希希望任意两个房间有且仅有一条路径可以相通(除非走了回头路)。小希现在把她的设计图给你,让你帮忙判断她的设计图是否符合她的设计思路。比如下面的例子,前两个是符合条件的,但是最后一个却有两种方法从5到达8。
在这里插入图片描述

Input
输入包含多组数据,每组数据是一个以0 0结尾的整数对列表,表示了一条通道连接的两个房间的编号。房间的编号至少为1,且不超过100000。每两组数据之间有一个空行。
整个文件以两个-1结尾。

Output
对于输入的每一组数据,输出仅包括一行。如果该迷宫符合小希的思路,那么输出"Yes",否则输出"No"。

Sample Input
6 8 5 3 5 2 6 4
5 6 0 0

8 1 7 3 6 2 8 9 7 5
7 4 7 8 7 6 0 0

3 8 6 8 6 4
5 3 5 6 5 2 0 0

-1 -1

Sample Output
Yes
Yes
No
问题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1272
问题简述: 中文题意
问题分析: 算是并查集入门水题吧, 用并查集连接所有的点,并且标记,当连接失败的时候直接No,最后判断一次标记过的点是否只在一个集合内即可
AC通过的C++语言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

int parent[100005];
bool vis[100005];

int Find(int x)
{
    if(parent[x]!=x)
    {
        parent[x]=Find(parent[x]);
    }
    return parent[x];
}

int flag=0;

void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        parent[fx]=fy;
    }
    else flag=1;
}

int main()
{
    ios::sync_with_stdio(false);
    int n,m;
    for(int i=0;i<100005;i++)
    {
        parent[i]=i;
        vis[i]=0;
    }
    while(cin>>n>>m&&n!=-1&&m!=-1)
    {
        if(n==0&&m==0)
        {
            cout<<"Yes"<<endl;
            continue;
        }
        flag=0;
        int u,v;
        join(n,m);
        vis[n]=1;
        vis[m]=1;
        while(cin>>u>>v)
        {
            if(u==0&&v==0) break;
            join(u,v);
            vis[u]=1;
            vis[v]=1;
        }
        if(flag==1)
        {
            cout<<"No"<<endl;
        }
        else
        {
            int co=0;
            for(int i=0;i<100005;i++)
            {
                if(vis[i]&&parent[i]==i)
                {
                    co++;
                }
            }
            if(co==1)
                cout<<"Yes"<<endl;
            else cout<<"No"<<endl;
        }
        for(int i=0;i<100005;i++)
        {
            parent[i]=i;
            vis[i]=0;
        }
    }
    return 0;
}

B
HDU - 1114 Piggy-Bank

没来得及做,貌似只要套个完全背包模板直接水过?

C
POJ - 1258 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
问题链接: http://poj.org/problem?id=1258
问题简述: 给定n,一下有n×n的牧场,求链接每个牧场的最小权值合
问题分析: 最小生成树入门,套个kruskal模板水过
AC通过的C++语言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

const int N=10005;
int parent[N];
struct road
{
    int a,b;
    int value;
}r[N];

int n,m,flag=0;
int Find(int x)
{
    if(parent[x]!=x)
    {
        parent[x]=Find(parent[x]);
    }
    return parent[x];
}

void join(int x,int y)
{
    int fx=Find(x);
    int fy=Find(y);
    if(fx!=fy)
    {
        parent[fx]=fy;
        flag=1;
    }
}

bool cmp(road x,road y)
{
    return x.value<y.value;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>m)
    {
        n=m*m;
        int g=1;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=m;j++)
            {
                road t;
                t.a=i;
                t.b=j;
                int k;
                cin>>k;
                t.value=k;
                r[g]=t;
                g++;
            }
        }
        g--;
        for(int i=1;i<=m;i++)
        {
            parent[i]=i;
        }
        int cr=0,sum=0;
        sort(r,r+g,cmp);
        for(int i=1;i<=n;i++)
        {
            flag=0;
            join(r[i].a,r[i].b);
            if(flag)
            {
                cr++;
                sum+=r[i].value;
            }
        }
        if(cr==m-1)
            cout<<sum<<endl;
        else cout<<-1<<endl;
        memset(parent,0,sizeof(parent));
        memset(r,0,sizeof(r));
    }
    return 0;
}

D
POJ - 1394 Railroad

百度了一下,权值线段树?看起来码量有点大,没做

E
POJ - 1979 Red and Black

There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
The end of the input is indicated by a line consisting of two zeros.
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
…#.
…#





#@…#
.#…#.
11 9
.#…
.#.#######.
.#.#…#.
.#.#.###.#.
.#.#…@#.#.
.#.#####.#.
.#…#.
.#########.

11 6
…#…#…#…
…#…#…#…
…#…#…###
…#…#…#@.
…#…#…#…
…#…#…#…
7 7
…#.#…
…#.#…
###.###
…@…
###.###
…#.#…
…#.#…
0 0
Sample Output
45
59
6
13
问题链接: http://poj.org/problem?id=1979
问题简述: 给定n行m列,.表示能到达,#表示无法到达,求从@出发,能到达的格子的数量
问题分析: BFS水题,DFS好像也能水过
AC通过的C++语言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

char maps[100][100];
bool vis[100][100];
int n,m;
int u,v;
int ans=1;
int xl[4][2]={0,1,0,-1,1,0,-1,0};

bool checkedge(int x,int y)
{
    if(maps[x][y]=='.'&&x>0&&x<=m&&y>0&&y<=n&&maps[x][y]!='#')
        return 1;
    return 0;
}

void dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int x1=x+xl[i][0];
        int y1=y+xl[i][1];
        if(checkedge(x1,y1))
        {
            maps[x1][y1]='#';
            ans++;
            dfs(x1,y1);
        }
    }
}

int main()
{
    ios::sync_with_stdio(false);

    while(cin>>n>>m&&n!=0&&m!=0)
    {
        ans=1;
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>maps[i][j];
                if(maps[i][j]=='@')
                {
                    u=i;
                    v=j;
                }
            }
        }
        dfs(u,v);
        cout<<ans<<endl;
        memset(vis,0,sizeof(vis));
    }
    return 0;
}

F
ZOJ - 2475 Benny’s Compiler

These days Benny has designed a new compiler for C programming language. His compilation system provides a compiler driver that invokes the language preprocessor, compiler, assembler and linker. C source file (with .C suffix) is translated to relocatable object module first, and then all modules are linked together to generate an executable object file.
The translator (preprocessor, compiler and assembler) works perfectly and can generate well optimized assembler code from C source file. But the linker has a serious bug – it cannot resolve global symbols when there are circular references. To be more specific, if file 1 references variables defined in file 2, file 2 references variables defined in file 3, … file n-1 references variables defined in file n and file n references variables defined in file 1, then Benny’s linker walks out because it doesn’t know which file should be processed first.
Your job is to determine whether a source file can be compiled successfully by Benny’s compiler.

Input

There are multiple test cases! In each test case, the first line contains one integer N, and then N lines follow. In each of these lines there are two integers Ai and Bi, meaning that file Ai references variables defined in file Bi (1 <= i <= N). The last line of the case contains one integer E, which is the file we want to compile.
A negative N denotes the end of input. Else you can assume 0 < N, Ai, Bi, E <= 100.

Output

There is just one line of output for each test case. If file E can be compiled successfully output “Yes”, else output “No”.

Sample Input

4
1 2
2 3
3 1
3 4
1
4
1 2
2 3
3 1
3 4
4
-1

Sample Output

No
Yes
问题链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2475
问题简述: 给定n,以下n行,每次表示从一个程序连接到另一个程序,如果最后连回了首个程序,则输出No,否则Yes
问题分析: 判断是否形成环,DFS水过
AC通过的C++语言程序如下:

#include<set>
#include<map>
#include<list>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#include<cstdio>
#include<string>
#include<bitset>
#include<vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define pi acos(-1)
#define mod 5
#define ll long long
#define ull unsigned long long

int n;
bool maps[1000][1000];
bool vis[1000];
int mb;
bool ans=0;

void dfs(int m)
{
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==1&&maps[m][i]==1)
        {
            ans=1;
            return;
        }
        if(maps[m][i]==1)
        {
            vis[i]=1;
            dfs(i);
            vis[i]=0;
        }
    }
    return;
}

int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n&&n!=-1)
    {
        ans=0;
        memset(maps,0,sizeof(maps));
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            int a,b;
            cin>>a>>b;
            if(a!=b)
            {
                maps[a][b]=1;
            }
        }
        cin>>mb;
        vis[mb]=1;
        dfs(mb);
        if(ans==1)
        {
            cout<<"No"<<endl;
        }
        else cout<<"Yes"<<endl;
    }
    return 0;
}

写不下了,貌似CSDN每篇字数有限制???下一篇继续

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值