Checking an Alibi(最短路径 Dijkstra)

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.

题意:题意是说有n个点,m条边,并且k个点上有牛,粮仓为1号点,让你求在p时间内有多少头牛可以到达粮仓,并且按照升序输出牛的编号。

思路:

Dijkstra算法求最短路径然后在于所要求的时间进行比较即可。

代码:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
int n,m;
int dis[1008],w[1008][1008],vis[1008],s[1008],z[1008];
void init()    //初始化
{
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
        if(i!=j)w[i][j]=inf;
        else w[i][j]=0;
}
void Dijkstra(int s)
{
    int pos,minn,i,j;
    for(i=0;i<=n;i++)dis[i]=w[s][i];   //对dis进行赋值,先是1到每个点的距离
    memset(vis,0,sizeof(vis));
    vis[s]=1;
    dis[s]=0;
    for(i=1;i<n;i++)
    {
        minn=inf;pos=0;
        for(j=1;j<=n;j++)
            if(!vis[j]&&minn>dis[j])  //查找可以更新的点
        {
            minn=dis[j];
            pos=j;
        }
       if(pos==0) break;
        vis[pos]=1;
        for(j=1;j<=n;j++)
          {
        if(!vis[j]&&dis[j]>w[pos][j]+minn)
            dis[j]=w[pos][j]+minn;
          }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    int k,p;
    while(cin>>n>>m>>k>>p)
    {
        init();
        int a,b,c;
        for(int i=0;i<m;i++)
        {
            cin>>a>>b>>c;
            if(w[a][b]>c)w[a][b]=w[b][a]=c;  //赋值操作
        }
        Dijkstra(1);  //起点为1,所以从1开始
        b=0;
        for(int i=1;i<=k;i++)
        {
            cin>>z[i];
            if(dis[z[i]]<=p)b++;   //符合条件的
        }
        cout<<b<<endl;
        for(int i=1;i<=k;i++)
        {
            if(dis[z[i]]<=p)   //从小到大输出
                cout<<i<<endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值