POJ 2394 Checking an Alibi (最短路+Dijkstra)

Checking an Alibi

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6217   Accepted: 2257

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

USACO 2005 March Silver

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

思路:很纯粹的最短路问题,求出每牛到1农场的距离 再和M比较一下

代码:16~32MS

我是完全函数化的一个模板做的。

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define M 501
#define INF 999999
#define min(a,b) (a<b?a:b)
int map[M][M],dis[M];
int P[110];
int n,m,f,p,c;
void Dijkstra()  //算法核心。
{
  bool cov[M];
  memset(cov,0,sizeof(cov));
  for(int i=1;i<=f;i++) dis[i]=(i==1?0:INF);
  for(int i=1;i<=f;i++)  //我是把起点也扔进去,不过就是多走一次循环。
  {
      int Min=INF,s;
      for(int y=1;y<=f;y++)
      if(!cov[y] && dis[y]<Min)
      {
          Min=dis[y];
          s=y;
      }
      cov[s]=1;
      for(int y=1;y<=f;y++)
      dis[y]=min(dis[y],dis[s]+map[s][y]);
  }
}
void Init()  //输入。
{
  int i,j,a,b,z;
  for(i=0;i<M;i++)
  for(j=0;j<M;j++)
  map[i][j]=(i==j?0:INF);
  scanf("%d%d%d%d",&f,&p,&c,&m);
  for(i=1;i<=p;i++)
  {
      scanf("%d%d%d",&a,&b,&z);
      map[a][b]=map[b][a]=min(map[a][b],z);
  }
  for(i=1;i<=c;i++)
    scanf("%d",&P[i]);
}
void output()  //输出。
{
    int i,j,n=0;
    for(i=1;i<=c;i++)
        if(dis[P[i]]<=m)
    {
        n++;
        P[i]=-1;
    }
    printf("%d\n",n);
    for(i=1;i<=c;i++)
        if(P[i]==-1)
        printf("%d\n",i);
}
int main()
{
    Init();
    Dijkstra();
    output();
    return 0;
}

POJ 2394 Checking an Alibi (最短路+Dijkstra)

时间: 2024-08-05 19:36:16

POJ 2394 Checking an Alibi (最短路+Dijkstra)的相关文章

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA)

POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意: 圣诞树是由n个节点和e个边构成的,点编号1-n,树根为编号1,选择一些边,使得所有节点构成一棵树,选择边的代价是(子孙的点的重量)×(这条边的价值).求代价最小多少. 分析: 单看每个点被计算过的代价,很明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 不过坑点还是很多的. 点的数量高达5w个,用矩阵存不行,只能用边存. 还有路径和结

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects

POJ - 1062 昂贵的聘礼(最短路Dijkstra)

昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u SubmitStatus Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:"嗯,如果你能够替我弄到大祭司的皮袄,我可以只要8000金币.如果你能够弄来他的水晶球,那么只要

poj 1062 昂贵的聘礼 最短路 dijkstra

#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <t

poj 1797 Heavy Transportation 【最短路Dijkstra 变式】

Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 23914   Accepted: 6355 Description Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 2387-Til the Cows Come Home(最短路Dijkstra+优先队列)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30007   Accepted: 10092 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135 建模分两部分:1.如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路 2.如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置 到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种 上代码 /**********************************

POJ 2387 Til the Cows Come Home dijkstra算法 用邻接表和邻接矩阵

题目如下: Til the Cows Come Home Time Limit: 1000MS        Memory Limit: 65536K Total Submissions: 27726        Accepted: 9353 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wa