POJ 1396 Checking an Alibi|Dijkstra

问题描述

总时间限制: 1000ms内存限制: 65536kB

描述

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.

输入

* 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.

输出

* 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.

样例输入

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

样例输出

4
1
2
3
4

问题解决

默写dijkstra算法

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int INF = 0xffffff0;
const int MAXN = 510;
 
int Map[MAXN][MAXN],Dist[MAXN],ans[MAXN];
bool vis[MAXN];
 
void Dijkstra(int N,int s)
{
    for(int i = 1; i <= N; ++i)
        if(i != s)
            Dist[i] = Map[s][i];
 
    Dist[s] = 0;
    vis[s] = 1;
    for(int i = 1; i <= N-1; ++i)
    {
        int Min = INF;
        int k = 0;
        for(int j = 1; j <= N; ++j)
        {
            if(!vis[j] && Dist[j] < Min)
            {
                Min = Dist[j];
                k = j;
            }
        }
        if(k == 0)
            return ;
        vis[k] = 1;
        for(int j = 1; j <= N; ++j)
        {
            if(!vis[j] && Map[k][j] != INF && Dist[j] > Dist[k]+Map[k][j])
                Dist[j] = Dist[k] + Map[k][j];
        }
    }
}
 
int main()
{
    int F,P,C,M,u,v,w,j;
    while(cin >> F >> P >> C >> M)
    {
        for(int i = 1; i <= F; ++i)
            for(int j = 1; j <= F; ++j)
                Map[i][j] = INF;
        for(int i = 1; i <= P; ++i)
        {
            cin >> u >> v >> w;
            if(Map[u][v] > w)
                Map[u][v] = Map[v][u] = w;
        }
        Dijkstra(F,1);

        int cnt = 0;
        for(int i = 1; i <= C; ++i)
        {
            cin >> j;
            if(Dist[j] <= M)
                ans[cnt++] = i;
        }
 
        cout << cnt << endl;
        for(int i = 0; i < cnt; ++i)
            cout << ans[i] << endl;
    }
 
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值