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每篇字數有限制???下一篇繼續

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章