Dijkstra求解最短路

来源:互联网 发布:2016网络播放器软件 编辑:程序博客网 时间:2024/06/06 02:25

poj2394:http://poj.org/problem?id=2394

Checking an Alibi
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6686 Accepted: 2432

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 81 4 21 2 12 3 63 5 55 4 61 7 914537

Sample Output

41234

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.

题意理解很费劲啊。。。。。大概就是说有F个农场C头牛,P条路,给出前M秒牛的位置,问有哪些牛能到达农场1偷吃粮食。

代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <map>#include <cmath>#include <vector>#include <algorithm>using namespace std;typedef long long  LL;const int MAXN = 0x3f3f3f3f;int f,p,c,m;int g[550][550];int dis[1100],vis[1100],V[150];void Dij(int x){    for(int i = 1; i <= f; i++)    {        dis[i] = MAXN;        vis[i] = 0;    }    dis[x] = 0;    for(int i = 1; i <= f; i++)    {        int MIN = MAXN;        int u = x;        for(int j = 1; j <= f; j++)        {            if(!vis[j] && dis[j] < MIN)            {                u = j;                MIN = dis[j];            }        }        vis[u] = 1;        for(int j = 1; j <= f; j++)        {            if(!vis[j] && (MIN + g[u][j]) < dis[j])            {                dis[j] = MIN + g[u][j];            }        }    }}int main(){//    freopen("in.txt","r",stdin);    while(~scanf("%d%d%d%d",&f,&p,&c,&m))    {        for(int i = 1; i <= f; i++)        {            for(int j = 1; j <= f; j++)                g[i][j] = (i == j?0:MAXN);        }        for(int i = 0; i < p; i++)        {            int a,b,w;            scanf("%d%d%d",&a,&b,&w);            g[a][b] = g[b][a] = min(g[a][b],w);   //  这里很坑啊,,,涨姿势了        }        int k = 0;        for(int i = 1; i <= c; i++)        {            int x;            scanf("%d",&x);            Dij(x);            if(dis[1] <= m)V[k++] = i;        }        printf("%d\n",k);        sort(V,V+k);        for(int i = 0; i < k; i++)            printf("%d\n",V[i]);    }    return 0;}


0 0
原创粉丝点击