码迷,mamicode.com
首页 > 其他好文 > 详细

codeforces 144 D. Missile Silos 最短路

时间:2015-08-30 19:35:30      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:最短路

链接:http://codeforces.com/problemset/problem/144/D

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 nm 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 viuiwi (1?≤?vi,?ui?≤?nvi?≠?ui,1?≤?wi?≤?1000), where viui 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 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.



题意:给你一个图。 问图中 有多少点  和起点的最短距离是len。边上的点也算。

做法:

把点最近距离 算好。

然后 边上 分两种情况。

if(l1<len&&l2<len&&l1+l2+dis==2*len)
ans++;

这种情况是,两边到这个点都是最短的。这个点只用计数一次。


if(l1<len&&l1+dis>len&&l2+(dis-(len-l1))>len)
ans++;
if(l2<len&&l2+dis>len&&l1+(dis-(len-l2))>len)
ans++;

这两种情况 就是讨论 在最短距离为len的点 在这条边上。 然后两边都判断下。


双向边重复计数了,所以边上的计算的答案 ans/=2


//http://codeforces.com/problemset/problem/144/D

#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <malloc.h>
#include <ctype.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <stack>
#include <queue>
#include <vector>
#include <deque>
#include <set>
#include <map>

using namespace std;


const int INF=0x3f3f3f3f;
const int MAXN=100010;
struct qnode
{
	int u;
	int v;
	int c;
	qnode(int _v=0,int _c=0):v(_v),c(_c){}
	bool operator <(const qnode &r)const
	{
		return c>r.c;
	}
}; 
struct Edge
{
	int u,v,cost;
	Edge(int _u=0,int _v=0,int _cost=0):u(_u),v(_v),cost(_cost){}
};
vector<Edge>E[MAXN];
bool vis[MAXN];
int dist[MAXN];
void Dijkstra(int n,int start)//点的编号从1开始
{
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=n;i++)dist[i]=INF;
	priority_queue<qnode>que;
	while(!que.empty())que.pop();
	dist[start]=0;
	que.push(qnode(start,0));
	qnode tmp;
	while(!que.empty())
	{
		tmp=que.top();
		que.pop();
		int u=tmp.v;
		if(vis[u])continue;
		vis[u]=true;
		for(int i=0;i<E[u].size();i++)
		{
			int v=E[tmp.v][i].v;
			int cost=E[u][i].cost;
			if(!vis[v]&&dist[v]>dist[u]+cost)
			{
				dist[v]=dist[u]+cost;
				que.push(qnode(v,dist[v]));
			}
		}
	}
}
void addedge(int u,int v,int w)
{
	E[u].push_back(Edge(u,v,w));
}

int main()
{
	int n,m,sta;
	while(scanf("%d%d%d",&n,&m,&sta)!=EOF)
	{
		memset(E,0,sizeof E);
		while(m--)
		{
			int u,v,cost;
			scanf("%d%d%d",&u,&v,&cost);
			addedge(u,v,cost);
			addedge(v,u,cost); 
		}
		__int64 len;
		scanf("%I64d",&len);
		Dijkstra(n,sta);
		int ans=0;

		
		for(int i=1;i<=n;i++)//所有的点
		{
			for(int j=0;j<E[i].size();j++)// 所有的边
			{
				int l1=dist[i];
				int l2=dist[E[i][j].v];

				int dis=E[i][j].cost;
				int v=E[i][j].v;
				if(l1<len&&l2<len&&l1+l2+dis==2*len)
					ans++;

				if(l1<len&&l1+dis>len&&l2+(dis-(len-l1))>len)
					ans++;

				if(l2<len&&l2+dis>len&&l1+(dis-(len-l2))>len)
					ans++;
			}
		}
		ans/=2; 
		for(int i=1;i<=n;i++)
		{
			if(dist[i]==len)
			{
			//	printf("dian %d\n",i);
				ans++;
			}
		}
		cout<<ans<<endl;

	}
	return 0;
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

codeforces 144 D. Missile Silos 最短路

标签:最短路

原文地址:http://blog.csdn.net/u013532224/article/details/48107217

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!