Codeforces 144D Missile Silos(SPFA最短路)

通过Bellman-Ford算法求解在一个由城市和道路组成的图中,距离首都为特定长度L的秘密导弹基地数量。需要考虑基地可能位于城市节点或道路中点的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:给定一个图,可以在边和点上设立发射井,其中发射井到首都的距离必须等于L,问可以建多少个发射井

思路:建图然后跑最短路,如果发射井在边上的话,画个图可以知道限制的条件(利用三角不等式),如果发射井在点上的话只需要判是否等于L即可,见代码


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 200005
#define LL long long
int cas=1,T;
int n,m,w;
const int INF = 0x3f3f3f3f  ;
struct Edge
{
	int from,to,dist;
	Edge(){}
	Edge(int u,int v,int d):from(u),to(v),dist(d){}
};
vector<Edge>edges;
vector<int> G[maxn];
int d[maxn];
int inq[maxn];
int cnt[maxn];
struct Bellman_ford
{
	void init()
	{
       for (int i = 0;i<=n;i++)
		   G[i].clear();
	   edges.clear();
	}
	void AddEdge(int from,int to,int dist)
	{
       edges.push_back(Edge(from,to,dist));
	   int mm = edges.size();
	   G[from].push_back(mm-1);
	}
	bool bellman_ford(int s)
	{
		queue<int>q;
		memset(inq,0,sizeof(inq));
		memset(cnt,0,sizeof(cnt));
		for (int i =0;i<=n;i++)
		   d[i]=INF;
		d[s]=0;
		inq[s]=1;
		q.push(s);
		while (!q.empty())
		{
			int u = q.front();q.pop();
			inq[u] = false;
			for (int i =0;i<G[u].size();i++)
			{
				Edge &e = edges[G[u][i]];
				if (d[u]<INF && d[e.to]>d[u]+e.dist)
				{
					d[e.to] = d[u] + e.dist;
					if (!inq[e.to])
					{
						q.push(e.to);
						inq[e.to]=1;
						if (++cnt[e.to]>n)
							return false;
					}
				}
			}
		}
		return true;
	}
};

int main()
{
	//freopen("in","r",stdin);	
	int s;
    scanf("%d%d%d",&n,&m,&s);
	Bellman_ford bf;
	bf.init();
	for (int i = 0;i<m;i++)
	{
		int u,v,t;
		scanf("%d%d%d",&u,&v,&t);
        bf.AddEdge(u,v,t);
		bf.AddEdge(v,u,t);
	}
	int l;
	scanf("%d",&l);
	bf.bellman_ford(s);
	int ans = 0;
    
    for (int i = 1;i<=n;i++)
	{
		for (int j = 0;j<G[i].size();j++)
		{
			Edge e = edges[G[i][j]];
			if (d[e.from] < l && l-d[e.from]<e.dist && (d[e.to]+e.dist-(l-d[e.from])> l))
			  ans++;
			if (d[e.to] < l && l-d[e.to]<e.dist && (d[e.from]+e.dist-(l-d[e.to])> l))
			  ans++;
           if (d[e.from] < l && d[e.to]<l && (d[e.from]+d[e.to]+e.dist==2*l))
	    	  ans++;
		}
	}
     ans = ans /2;                 //对于每一条边计算了两次,所以除二
	for (int i = 1;i<=n;i++)
		if (d[i]==l)
			ans++;
	printf("%d\n",ans);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}


题目

Description

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 ≤ 1051 ≤ 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 ≠ ui1 ≤ 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 Input

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

Hint

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





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值