bzoj:1681 [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.
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.
第1行输入四个整数F,只C,和M;
接下来P行每行三个整数描述一条路,起点终点和通过时间.
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
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
1
2
3
4
![](https://images2015.cnblogs.com/blog/841250/201511/841250-20151126202001671-746061700.png)
![](https://images2015.cnblogs.com/blog/841250/201511/841250-20151126202046609-855465556.png)
#include<cstdio> #include<algorithm> using namespace std; struct na{ int y,z,ne; na(){ ne=0; } }; struct dui{ int v,n; }; const int INF=10000000; int n,c,m,l[2001],r[2001],x,y,z,pp,num=0,p[501],nu,dis[501],an=0; bool ans[101]; na b[2001]; dui d[501]; char cc; int read(){ int a=0; cc=getchar(); while(cc<'0'||cc>'9') cc=getchar(); while(cc>='0'&&cc<='9') a=a*10+cc-48,cc=getchar(); return a; } void in(int x,int y,int z){ num++; if (l[x]==0) l[x]=num;else b[r[x]].ne=num; b[num].y=y;b[num].z=z; r[x]=num; } void sw(int a,int b){ swap(p[d[a].n],p[d[b].n]); swap(d[a],d[b]); } void utz(int x){ while(x>1&&d[x].v<d[x>>1].v) sw(x,x>>1),x>>=1; } void dtz(int j){ j=j*2; while(j<=nu){ if (j<nu&&d[j].v>d[j+1].v) j++; sw(j>>1,j); j*=2; } sw(j>>1,nu); p[d[nu].n]=0; nu--; } int main(){ n=read();pp=read();m=read();c=read(); nu=n; for (int i=1;i<=pp;i++){ x=read();y=read();z=read(); in(x,y,z);in(y,x,z); } for (int i=1;i<=n;i++) dis[i]=d[i].v=INF,d[i].n=i,p[i]=i; d[1].v=0; for (int i=1;i<=n;i++){ x=d[1].n; if (d[1].v>c) break; dis[x]=d[1].v; dtz(1); for (int j=l[x];j;j=b[j].ne) if (p[b[j].y]&&d[p[b[j].y]].v>dis[x]+b[j].z&&dis[x]+b[j].z<=c){ d[p[b[j].y]].v=dis[x]+b[j].z; utz(p[b[j].y]); } } for (int i=1;i<=m;i++){ x=read(); if (dis[x]<=c)an++,ans[i]=1; } printf("%d\n",an); for (int i=1;i<=m;i++) if (ans[i]) printf("%d\n",i); }