• POJ 2394 Checking an Alibi


    Checking an Alibi
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5685   Accepted: 2060

    Description

    A crime has been comitted: a load of grain has been taken from the barn by one of FJ's cows. FJ is trying to determine which of his C (1 <= C <= 100) cows is the culprit. Fortunately, a passing satellite took an image of his farm M (1 <= M <= 70000) seconds before the crime took place, giving the location of all of the cows. He wants to know which cows had time to get to the barn to steal the grain. 

    Farmer John's farm comprises F (1 <= F <= 500) fields numbered 1..F and connected by P (1 <= P <= 1,000) bidirectional paths whose traversal time is in the range 1..70000 seconds (cows walk very slowly). Field 1 contains the barn. It takes no time to travel within a field (switch paths). 

    Given the layout of Farmer John's farm and the location of each cow when the satellite flew over, determine set of cows who could be guilty. 

    NOTE: Do not declare a variable named exactly 'time'. This will reference the system call and never give you the results you really want.

    Input

    * Line 1: Four space-separated integers: F, P, C, and M 

    * Lines 2..P+1: Three space-separated integers describing a path: F1,F2, and T. The path connects F1 and F2 and requires T seconds to traverse. 

    * Lines P+2..P+C+1: One integer per line, the location of a cow. The first line gives the field number of cow 1, the second of cow 2, etc.

    Output

    * Line 1: A single integer N, the number of cows that could be guilty of the crime. 

    * Lines 2..N+1: A single cow number on each line that is one of the cows that could be guilty of the crime. The list must be in ascending order.

    Sample Input

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

    Sample Output

    4
    1
    2
    3
    4

    Hint

    INPUT DETAILS: 

    Fields/distances like this: 
              6
    
    4------5
    | |
    2| |
    | |
    7-----1 |5
    9 | |
    1| |
    | |
    2------3

    OUTPUT DETAILS: 

    Any cow except cow 5 could have done it. Cow 5 would take 9 seconds to get to the barn.

    Source

     
     
    题意:有F个农场 C头牛,P条双向路 给出M秒前C头牛所在的农场位置 问 M后谁有可能到达1农场偷吃粮食。

    思路:很纯粹的最短路问题,求出每牛到1农场的距离 再和M比较一下
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    
    using namespace std;
    
    const int VM=520;
    const int EM=500010;
    const int INF=0x3f3f3f3f;
    
    struct Edge{
        int to,nxt;
        int cap;
    }edge[EM];
    
    struct node{
        int point,cost;
        bool operator < (const node &a)const{
            return a.cost<cost;
        }
    };
    
    int F,P,C,M;
    int cnt,head[VM];
    int vis[VM],dis[VM],ans[VM];
    
    void addedge(int cu,int cv,int cw){
        edge[cnt].to=cv;    edge[cnt].cap=cw;   edge[cnt].nxt=head[cu];
        head[cu]=cnt++;
        edge[cnt].to=cu;    edge[cnt].cap=cw;   edge[cnt].nxt=head[cv];
        head[cv]=cnt++;
    }
    
    void Dijkstra(){ //dij 用队列优先实现
        node tmp;
        priority_queue<node> q;
        while(!q.empty())
            q.pop();
        memset(vis,0,sizeof(vis));
        memset(dis,0x3f,sizeof(dis));
        /* 相当于下面
            for(int i=0;i<VM;i++)
                dis[i]=INF;
        */
        dis[1]=0;
        tmp.point=1;
        tmp.cost=0;
        q.push(tmp);
        while(!q.empty()){
            int u=q.top().point;
            q.pop();
            vis[u]=1;
            for(int i=head[u];i!=-1;i=edge[i].nxt){
                int v=edge[i].to;
                if(!vis[v] && dis[v]>dis[u]+edge[i].cap){
                    dis[v]=dis[u]+edge[i].cap;
                    tmp.point=v;
                    tmp.cost=dis[v];
                    q.push(tmp);
                }
            }
        }
    }
    
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d%d%d%d",&F,&P,&C,&M)){
            cnt=0;
            memset(head,-1,sizeof(head));
            int u,v,w;
            while(P--){
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
            }
            Dijkstra();
            int k=0,loc;
            for(int i=1;i<=C;i++){
                scanf("%d",&loc);
                if(dis[loc]<=M)
                    ans[k++]=i;
            }
            printf("%d\n",k);
            for(int i=0;i<k;i++)
                printf("%d\n",ans[i]);
        }
        return 0;
    }
  • 相关阅读:
    too many open files linux服务器 golang java
    fasthttp 文档手册
    syncer.go
    grpc.go
    stm.go
    session.go
    mutex.go
    [HTML5]label标签使用以及建议
    禁止使用finalize方法
    [支付宝]手机网站支付快速接入
  • 原文地址:https://www.cnblogs.com/jackge/p/3051215.html
Copyright © 2020-2023  润新知