zoukankan      html  css  js  c++  java
  • (离线处理+BFS) poi Tales of seafaring

    Tales of seafaring

    Memory limit: 128 MB

    Young Bytensson loves to hang out in the port tavern, where he often listens to the sea dogs telling their tales of seafaring. Initially, he believed them all, however incredible they sounded. Over time though, he became suspicious. He has decided to write a program that will verify if there may be any grain of truth in those tall stories. Bytensson reasoned that while he cannot tell if the sailors indeed weathered all those storms, he can at least find out if their travel itineraries make sense. This is a task for a programmer, which Bytensson, unfortunately, is not. Help him out!

    There are  ports and  waterways connecting them in the waters frequented by the sailors Bytensson listened to. If there is a waterway between two ports, then sailing from one to the other is possible. Any waterway can be sailed in both directions.

    Bytensson got to know  seafaring tales. Each tells of a sailor who began his journey in one port, sailed a number of waterways, and ended up in another port, which may have been the one he initially set sail from. The sailor in question may have sailed through the same waterway many times, each time in any direction.

    Input

    In the first line of the standard input, there are three integers, , and  (). These denote, respectively: the number of ports in the waters frequented by the sailors who told Bytensson their stories, the number of waterways, and the number of tales.

    The  lines that follow specify the waterways. A single waterway's description consists of a single line that contains two integers,  and  (), separated by a single space; these specify the numbers of ports at the two ends of this particular waterway.

    The  lines that follow specify the tales that Bytensson has heard. A single tale's description consists of a single line with three integers, , and  (), separated by single spaces. These indicate that the tale's protagonist set sail from port no. , ended the journey in port no. , and sailed exactly  times through various waterways.

    In tests worth 50% of the total points, the additional condition  holds.

    Output

    Your program should print exactly  lines to the standard output; the -th of them should contain the word TAK (Polish for yes) if the journey described in the -th tale (in input order) could have taken place. If it could not, then the line should contain the word NIE (Polish for no).

    Example

    For the input data:

    8 7 4
    1 2
    2 3
    3 4
    5 6
    6 7
    7 8
    8 5
    2 3 1
    1 4 1
    5 5 8
    1 8 10
    

    the correct result is:

    TAK
    NIE
    TAK
    NIE
    

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<cstdlib>
    #include<algorithm>
    #include<queue>
    #include<vector>
    using namespace std;
    int n,m,k;
    vector<int> e[5005];
    struct node
    {
        int id,s,t,w,ans;
    }query[1000010];
    bool vis[5005];
    int dist[5005][2];
    bool cmp1(node a,node b)
    {
        return a.s<b.s;
    }
    bool cmp2(node a,node b)
    {
        return a.id<b.id;
    }
    void bfs(int s)
    {
        memset(dist,-1,sizeof(dist));
        dist[s][0]=0;
        queue<int> q;
        q.push(s),q.push(0);
        while(!q.empty())
        {
            int x,flag;
            x=q.front(),q.pop();
            flag=q.front(),q.pop();
            for(int i=0;i<e[x].size();i++)
            {
                int v=e[x][i];
                if(dist[v][flag^1]==-1)
                {
                    dist[v][flag^1]=dist[x][flag]+1;
                    q.push(v);
                    q.push(flag^1);
                }
            }
        }
    }
    int main()
    {
        scanf("%d%d%d",&n,&m,&k);
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            e[x].push_back(y);
            e[y].push_back(x);
            vis[x]=vis[y]=1;
        }
        for(int i=1;i<=k;i++)
        {
            query[i].id=i;
            int s,t,w;
            scanf("%d%d%d",&s,&t,&w);
            query[i].s=s,query[i].t=t,query[i].w=w;
        }
        sort(query+1,query+1+k,cmp1);
        for(int i=1;i<=k;i++)
        {
            int temp=i;
            bfs(query[i].s);
            while(i<=k&&query[i].s==query[temp].s)
            {
                if(query[i].s==query[i].t&&!vis[query[i].s])
                    query[i].ans=0;
                else
                {
                    if(query[i].w&1)
                    {
                        if(query[i].w>=dist[query[i].t][1]&&dist[query[i].t][1]!=-1)
                            query[i].ans=1;
                        else
                            query[i].ans=0;
                    }
                    else
                    {
                        if(query[i].w>=dist[query[i].t][0]&&dist[query[i].t][0]!=-1)
                            query[i].ans=1;
                        else
                            query[i].ans=0;
                    }
                }
                i++;
            }
            i--;
        }
        sort(query+1,query+1+k,cmp2);
        for(int i=1;i<=k;i++)
        {
            if(query[i].ans)
                printf("TAK
    ");
            else
                printf("NIE
    ");
        }
        return 0;
    }
    

      

    Sample grading tests:

    • 1ocen, each pair of ports connected, a million random tales, each with answer TAK;
    • 2ocen, journeys possible between ports of the same parity, a million random tales, half with answer TAK, half with answer NIE.
  • 相关阅读:
    PHP函数---$_Get()和$_Post()的用法
    python mysqldb 教程
    python+ mysql存储二进制流的方式
    Android项目 手机安全卫士(代码最全,注释最详细)之七 应用程序的更新安装
    约瑟夫环的两种实现
    80386的各种寄存器一览
    hdu 4741 Save Labman No.004 [2013年杭州ACM网络赛]
    物理DG主备库切换时遇到ORA-16139: media recovery required错误
    c# 获取全屏 中鼠标焦点的位置坐标
    hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)
  • 原文地址:https://www.cnblogs.com/water-full/p/4518820.html
Copyright © 2011-2022 走看看