[BZOJ1681]&&[Usaco2005 Mar]Checking an Alibi 不在场的证明

描述 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.

谷仓里发现谷物被盗!约翰正试图从 C(1≤C≤100) 只奶牛里找出那个偷谷物的罪犯.幸运的是,一个恰好路过的卫星拍下谷物被盗前 M(1≤M≤70000) 秒的农场的图片.这样约翰就能通过牛们的位置来判断谁有足够的时间来盗窃谷物.

约翰农场有 F(1≤F≤500) 草地,标号 1 到 F,还有 P(1≤P≤1000) 条双向路连接着它们.通过这些路需要的时间在 1 到 70000 秒的范围内.田地 1 上建有那个被盗的谷仓. 给出农场地图,以及卫星照片里每只牛所在的位置.请判断哪些牛有可能犯罪.

描述一条路,起点终点和通过时间.

接下来 C 行每行一个整数,表示一头牛所在的地点.

输出格式 OutputFormat

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.

第 1 行输出嫌疑犯的数目,接下来一行输出一只嫌疑犯的编号.

样例输入 SampleInput

3 2 10 4
1 2 3
3 1 10
2
2
2
2
3
3
3
2
2
2

样例输出 SampleOutput

7
1
2
3
4
8
9
10

来源 Source

Silver


BZOJ 1681


代码 Code

最短路。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int inf=0x7fffffff/11.27;
int i,j,t,n,m,l,r,k,z,y,x;
inline void read(int &x)
{
    x=0;char ch=getchar();
    while (ch<'0' || ch>'9') ch=getchar();
    while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
}
#include <queue>
struct edge
{
    int to,next,val;
}e[2005];
int head[505],dis[505];
bool used[505],can[105];
deque <int> q;
int c,f,p,cnt,ans;
inline void ins(int u,int v,int w)
{
    e[++cnt].to=v;e[cnt].next=head[u];e[cnt].val=w;
    head[u]=cnt;
}
inline void spfa(int s)
{
    int i,j,t,u,v,w;
    memset(used,false,sizeof(used));
    while (!q.empty()) q.pop_front();
    for (i=1;i<=f;i++) dis[i]=inf;
    used[s]=true;dis[s]=0;q.push_back(s);
    while (!q.empty())
    {
        u=q.front();q.pop_front();used[u]=false;
        for (i=head[u];i;i=e[i].next)
        {
            v=e[i].to;w=e[i].val;
            if (dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if (!used[v])
                {
                    used[v]=true;
                    if (!q.empty() && dis[q.front()]>dis[v]) q.push_front(v);
                    else q.push_back(v);
                }
            }
        }
    }
}
int main()
{
    cnt=0;memset(can,false,sizeof(can));
    read(f);read(p);read(c);read(m);
    for (i=1;i<=p;i++)
    {
        read(x);read(y);read(z);
        ins(x,y,z);
        ins(y,x,z);
    }
    spfa(1);
    for (i=1;i<=c;i++)
    {
        read(x);
        if (dis[x]<=m)
        {
            ans++;
            can[i]=true;
        }
    }
    printf("%d\n",ans);
    for (i=1;i<=c;i++) if (can[i]) printf("%d\n",i);
    return 0;
}