您的位置:首页 > 其它

Codeforces 144D. Missile Silos 最短路

2013-04-08 15:31 459 查看
D. Missile Silos

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

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 33 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 1from 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 distance3 from city 4 in the direction to city 5 and
at a distance 3 from city 5 to city 4.

已知导弹发射井(?)距离首都s的最短距离为l,求出导弹发射井的数量。

首先求出每个顶点到首都的距离dis[i]。若dis[i]==len则ans++。

然后枚举每条边,进行如下计算:

if ( (dis[u]<len) && (len-dis[u]<w) && (w-(len-dis[u])>len-dis[v]) ) ans++;导弹位于该条边靠近顶点u的位置

if ( (dis[v]<len) && (len-dis[v]<w) && (w-(len-dis[v])>len-dis[u]) ) ans++;导弹位于该条边靠近顶点v的位置

if ( (dis[v]<len) && (dis[u]<len) && (dis[u]+dis[v]+w==len*2) ) ans++;导弹位于顶点u,v之间

最后ans即为导弹数量。。。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

const int OO=1e9+9;
const int maxn=222222;
const int INF=1e9+7;

struct hentai{
    int to;
    int next;
    int flow;
}edges[maxn];

struct death{
    int u;
    int v;
    int w;
}link[maxn];
int cnt;

int outque[maxn];
int head[maxn];
int edge;
int node,src;
int n,m;
int dis[maxn];
bool v[maxn];
int len;

queue<int>que;

void init(int _node,int _src)
{
    node=_node;
    src=_src;
    for (int i=0;i<=node;i++)
    {
        head[i]=-1;
        dis[i]=OO;
        v[i]=false;
    }
    edge=0;
    cnt=0;
}

void addedge(int u,int v,int w)
{
    edges[edge].flow=w;edges[edge].to=v;edges[edge].next=head[u];head[u]=edge++;
    edges[edge].flow=w;edges[edge].to=u;edges[edge].next=head[v];head[v]=edge++;
    link[cnt].u=u;link[cnt].v=v;link[cnt].w=w;cnt++;
}

bool SPFA()
{
    int top;
    for (int i=0;i<=node;i++)
    {
        dis[i]=INF;
    }
    memset(v,0,sizeof(v));
    memset(outque,0,sizeof(outque));
    while (!que.empty()) que.pop();
    que.push(src);
    v[src]=true;
    dis[src]=0;
    while (!que.empty())
    {
        top=que.front();
        que.pop();
        v[top]=false;
        outque[top]++;
        if (outque[top]>node) return false;
        int k=head[top];
        while (k!=-1)
        {
            if ( dis[edges[k].to]==INF||dis[edges[k].to]>dis[top]+edges[k].flow )
            {
                dis[edges[k].to]=dis[top]+edges[k].flow;
                if (!v[edges[k].to])
                {
                    v[edges[k].to]=true;
                    que.push(edges[k].to);
                }
            }
            k=edges[k].next;
        }
    }
    return true;
}

int main()
{
    cin>>n>>m>>src;
    init(n,src);
    for (int i=1;i<=m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        addedge(u,v,w);
    }
    cin>>len;

    //SPFA
    SPFA();

    int ans=0;
    for (int i=1;i<=node;i++)
    {
        if (dis[i]==len) ans++;
    }

    //(u,v)
    int u,v,w;
    for (int i=0;i<cnt;i++)
    {
        u=link[i].u;
        v=link[i].v;
        w=link[i].w;
        if ( (dis[u]<len) && (len-dis[u]<w) && (w-(len-dis[u])>len-dis[v]) ) ans++;
        if ( (dis[v]<len) && (len-dis[v]<w) && (w-(len-dis[v])>len-dis[u]) ) ans++;
        if ( (dis[v]<len) && (dis[u]<len) && (dis[u]+dis[v]+w==len*2) ) ans++;
    }
    //over

    cout<<ans<<endl;
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: