/*
*State: 2394    Accepted    724K    16MS    G++    1789B    
*题目大意:
*        给定无向图,输入的c代表c个奶牛,然后输入c个点的位置,
*        求能够在t时间内到达点1的牛。
*解题思路:
*        直接spfa吧。
*/
View Code
#include <iostream>
#include <queue>
using namespace std;

const int MAXN = 505;
const int MAXE = 1024;
const int inf = 0x3f3f3f3f;

typedef struct _node
{
    int v, w;
    int next;
}N;

N edge[2 * MAXE];
int head[MAXN], cntEdge, endDis[MAXN];
int dis[MAXN];

void init(int n)
{
    cntEdge = 0;
    for(int i = 0; i <= n; i++)
    {
        head[i] = -1;
    }
}

void addEdge(int u, int v, int w)
{
    edge[cntEdge].v = v;
    edge[cntEdge].w = w;
    edge[cntEdge].next = head[u];
    head[u] = cntEdge++;

    edge[cntEdge].v = u;
    edge[cntEdge].w = w;
    edge[cntEdge].next = head[v];
    head[v] = cntEdge++;
}


void spfa(int s, int n)//dir == 1反图
{
    int vst[MAXN] = {0};
    for(int i = 0; i <= n; i++)
        dis[i] = inf;

    queue<int> Q;
    Q.push(s);
    vst[s] = 1;
    dis[s] = 0;

    while(!Q.empty())
    {
        int pre = Q.front();
        Q.pop();
        vst[pre] = 0;

        for(int f = head[pre]; f != -1; f = edge[f].next)
        {
            int son = edge[f].v, w = edge[f].w;
            if(dis[pre] + w < dis[son])
            {
                dis[son] = dis[pre] + w;
                if(!vst[son])
                {
                    vst[son] = 1;
                    Q.push(son);
                }
            }
        }
    }

    return ;
}

int main(void)
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    int n, m, c, t;
    while(scanf("%d %d %d %d", &n, &m, &c, &t) == 4)
    {
        init(n);
        int u, v, w;
        for(int i = 0; i < m; i++)
        {
            scanf("%d %d %d", &u, &v, &w);
            addEdge(u, v, w);
        }
        spfa(1, n);
        int ans[MAXN], cnt = 0;
        for(int i = 1; i <= c; i++)
        {
            int s;
            scanf("%d", &s);
            if(dis[s] <= t)
                ans[cnt++] = i;
        }
        printf("%d\n", cnt);
        for(int i = 0; i < cnt; i++)
            printf("%d\n", ans[i]);
    }
    return 0;
}
posted on 2012-08-18 09:21  cchun  阅读(249)  评论(0编辑  收藏  举报