Codeforces Round #103 (Div. 2)---D. Missile Silos(最短路+枚举边)

A country called Berland consists of n cities, numbered with integer numbers from 1 to n. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Missiles. The exact position of the Super Duper Secret Missile Silos is kept secret but Bob managed to get hold of the information. That information says that all silos are located exactly at a distance l from the capital. The capital is located in the city with number s.

The documents give the formal definition: the Super Duper Secret Missile Silo is located at some place (which is either city or a point on a road) if and only if the shortest distance from this place to the capital along the roads of the country equals exactly l.

Bob wants to know how many missile silos are located in Berland to sell the information then to enemy spies. Help Bob.
Input

The first line contains three integers n, m and s (2 ≤ n ≤ 105, , 1 ≤ s ≤ n) — the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. s.

Then m lines contain the descriptions of roads. Each of them is described by three integers vi, ui, wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 1 ≤ wi ≤ 1000), where vi, ui are numbers of the cities connected by this road and wi is its length. The last input line contains integer l (0 ≤ l ≤ 109) — the distance from the capital to the missile silos. It is guaranteed that:

between any two cities no more than one road exists;
each road connects two different cities;
from each city there is at least one way to any other city by the roads. 

Output

Print the single number — the number of Super Duper Secret Missile Silos that are located in Berland.
Sample test(s)
Input

4 6 1
1 2 1
1 3 3
2 3 1
2 4 1
3 4 1
1 4 2
2

Output

3

Input

5 6 3
3 1 1
3 2 1
3 4 1
3 5 1
1 2 6
4 5 8
4

Output

3

Note

In the first sample the silos are located in cities 3 and 4 and on road (1, 3) at a distance 2 from city 1 (correspondingly, at a distance 1 from city 3).

In the second sample one missile silo is located right in the middle of the road (1, 2). Two more silos are on the road (4, 5) at a distance 3 from city 4 in the direction to city 5 and at a distance 3 from city 5 to city 4.

求最短路,然后枚举边

/************************************************************************* > File Name: CF-103-D.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年03月25日 星期三 11时07分13秒 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int N = 100100;

int head[N], tot;
int dist[N];
map <PLL, bool> mp;

struct node
{
    int ID;
    int weight;
    int to;
    int next;
}edge[N << 1];

void addedge(int from, int to, int weight, int ID)
{
    edge[tot].to = to;
    edge[tot].weight = weight;
    edge[tot].ID = ID;
    edge[tot].next = head[from];
    head[from] = tot++;
}

void spfa(int s)
{
    memset(dist, inf, sizeof(dist));
    dist[s] = 0;
    queue <int> qu;
    qu.push(s);
    while (!qu.empty())
    {
        int u = qu.front();
        qu.pop();
        for (int i = head[u]; ~i; i = edge[i].next)
        {
            int v = edge[i].to;
            if (dist[v] > dist[u] + edge[i].weight)
            {
                dist[v] = dist[u] + edge[i].weight;
                qu.push(v);
            }
        }
    }
}

int main()
{
    int n, m, s;
    while (~scanf("%d%d%d", &n, &m, &s))
    {
        int u, v, w, l;
        memset(head, -1, sizeof(head));
        tot = 0;
        mp.clear();
        for (int i = 1; i <= m; ++i)
        {
            scanf("%d%d%d", &u, &v, &w);
            addedge(u, v, w, i);
            addedge(v, u, w, i + m);
        }
        scanf("%d", &l);
        spfa(s);
        int ans = 0;
        for (int u = 1; u <= n; ++u)
        {
            ans += (dist[u] == l);
        }
        for (int u = 1; u <= n; ++u)
        {
            for (int i = head[u]; ~i; i = edge[i].next)
            {
                int ID = edge[i].ID;
                int ID2 = (ID <= m ? ID + m : ID - m);
                int v = edge[i].to;
                int x1 = l - dist[u];
                if (x1 < edge[i].weight && x1 > 0)
                {
                    if (dist[v] - x1 + edge[i].weight >= l)
                    {
                        if (!mp[make_pair(ID, x1)] && !mp[make_pair(ID2, edge[i].weight - x1)])
                        {
                            ++ans;
                            mp[make_pair(ID, x1)] = mp[make_pair(ID2, edge[i].weight - x1)] = 1;
                        }
                    }
                }
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(图论,最短路)