Codeforces 449B Jzzhu and Cities(最短路计数+迪杰斯特拉最小堆优化)

Codeforces题:求可关闭火车路线最大数量
博客围绕Codeforces的一道题展开,该国n个城市,1号为首都,有m条道路和k条火车路线。为节省资金,要在保证各城市到首都最短路径不变的情况下关闭火车路线。解题需建图求单源最短路,比较最短路与火车路线距离,还涉及最短路计数,数据大时要用邻接表和迪杰斯特拉最小堆优化。

B. Jzzhu and Cities
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1 is the capital of A. Also there are m roads connecting the cities. One can go from city ui to vi (and vise versa) using the i-th road, the length of this road is xi. Finally, there are k train routes in the country. One can use the i-th train route to go from capital of the country to city si (and vise versa), the length of this route is yi.

Jzzhu doesn’t want to waste the money of the country, so he is going to close some of the train routes. Please tell Jzzhu the maximum number of the train routes which can be closed under the following condition: the length of the shortest path from every city to the capital mustn’t change.

Input

The first line contains three integers n, m, k (2 ≤ n ≤ 105; 1 ≤ m ≤ 3·105; 1 ≤ k ≤ 105).

Each of the next m lines contains three integers ui, vi, xi (1 ≤ ui, vi ≤ n; ui ≠ vi; 1 ≤ xi ≤ 109).

Each of the next k lines contains two integers si and yi (2 ≤ si ≤ n; 1 ≤ yi ≤ 109).

It is guaranteed that there is at least one way from every city to the capital. Note, that there can be multiple roads between two cities. Also, there can be multiple routes going to the same city from the capital.
Output

Output a single integer representing the maximum number of the train routes which can be closed.
Examples

input

5 5 3
1 2 1
2 3 2
1 3 3
3 4 4
1 5 5
3 5
4 5
5 5

Output

2

Input

2 2 3
1 2 2
2 1 3
2 1
2 2
2 3

Output

2

题目大意:这个国家有n个城市,编号1-n,编号1的城市为首都,城市之间有m条双向道路连接,同时有k条火车路线可以直接让其他城市与首都城市连接,每条火车路线也存在自己的距离长度,因为火车路线的修建存在一定花费,现在需要保证首都在其余的n-1个城市的距离最短的情况下,去除一些没没必要的火车路径,问最多能去除几条。

解题思路:因为求单源最短路,所以很简单的直接把火车路线和普通的公路路线全部建图,然后从源点1开始求到其他点的最短路,但是我们需要知道最终求出的最短路有哪些火车路线是参与的,把没有参与的火车路线统计数目就是答案,因为火车路线是源点1直接通向其他城市的路线,而题目也是问源点1到其他城市的最短路,所以我们可以把最后求出的到每个城市的最短路dis[maxn]与火车路线的距离进行比较,如果某个点v,最短路dis[v]小于源点到这个点v的火车路线w,就说明此时的这条火车路线根本不可能参与,所以去除,ans++,如果某个点v,最短路dis[v]等于源点到这个点v的火车路线w,就需要做特殊判断,看看有没有到终点v有没有其他路线,如果有其他路线可以到达,那么此时的这条火车路线也是没必要的,就去除,因为我们需要知道有没有多余的路线,就涉及了最短路计数问题,这里的最短路需要注意:
常知的最短路计数方程:
if(dis[v]==dis[u]+mp[u][v])
num[v]=num[v]+num[u];
else if(dis[v]>dis[u]+mp[u][v])
{
dis[v]=dis[u]+mp[u][v];
num[u]=num[v];
}
但是这个题目中我们需要知道的是从源点直接到终点v的最短路数目,
所以此时的最短路计数方程是:
if(dis[v]==dis[u]+mp[u][v])
num[v]++;
else if(dis[v]>dis[u]+mp[u][v])
{
dis[v]=dis[u]+mp[u][v];
//1表示源点到源点的最短路只有1条
num[v]=1;
}
然后这题数据比较大,还需要用邻接表储存然后用迪杰斯特拉最小堆优化,不然会严重超时

#include<iostream>
#include<queue>
#include<string.h>
#include<string>
using namespace std;
typedef long long ll;
const int N=3e5+100;
struct Node{
	int v,next;
	ll w;
}edge[4*N];
int head[N],cnt;
void add_edge(int u,int v,ll w)
{
	edge[cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}
bool vis[N];
ll dis[N];
int num[N],n,m,k;
struct Sode{
	int v;
	ll dis;
	Sode(int v1,ll dis1)
	{
		v=v1;
		dis=dis1;
	}
	bool operator<(const Sode &t)const
	{
		return dis>t.dis;
	}
};
priority_queue<Sode>q;
void dij()
{
	while(!q.empty())
	q.pop();
	memset(vis,0,sizeof(vis));
	memset(num,0,sizeof(num));
	for(int i=1;i<=n;i++)
	dis[i]=1e18;
	dis[1]=0;
	q.push(Sode(1,0));
//	num[1]=1;
	while(!q.empty())
	{
		Sode t=q.top();
		q.pop();
		if(vis[t.v])continue;
		vis[t.v]=true;
		for(int i=head[t.v];i!=-1;i=edge[i].next)
		{
			int v=edge[i].v;
			ll w=edge[i].w;
			//开始最短路计数
			if(dis[v]==dis[t.v]+w)
			num[v]++;
			else if(dis[v]>dis[t.v]+w)
			{
				num[v]=1;
				dis[v]=dis[t.v]+w;
				q.push(Sode(v,dis[v]));
			}
		}
	}
}
vector<int>inv;
vector<ll>inw;
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	memset(head,-1,sizeof(head));
	cnt=0;
	while(m--)
	{
		int x,y;
		ll w;
		scanf("%d%d%I64d",&x,&y,&w);
		add_edge(x,y,w);
		add_edge(y,x,w);
	}
	inv.clear();
	inw.clear();
	int ans=0;
	while(k--)
	{
		int v;
		ll w;
		scanf("%d%I64d",&v,&w);
		inv.push_back(v);
		inw.push_back(w);
		add_edge(1,v,w);
		add_edge(v,1,w);
	}
	dij();
	for(int i=0;i<inv.size();i++)
	{
		int v=inv[i];
		ll w=inw[i];
		if(dis[v]<w)
		ans++;
		else if(dis[v]==w)
		{
			//这里注意,因为题目中到终点v的火车路线可能有多条,所以需要num[v]--判断多个终点为v的火车路线
			if(num[v]>1)
			{
				ans++;
				num[v]--;
			}
		}
	}
	printf("%d\n",ans);
}


### Codeforces 平台上的短路径算法题目 #### Dijkstra 算法的应用实例 当面对构建短路径树的需求时,可以采用Dijkstra算法来解决。该方法的核心在于优先选择当前节点能够延伸出去的多条候选边中具有最小权重的一条作为扩展方向;而在仅存在唯一一条可能的拓展边的情况下,则直接选取这条边继续探索过程[^1]。 ```python import heapq def dijkstra(graph, start): n = len(graph) dist = [float('inf')] * n dist[start] = 0 heap = [(0, start)] while heap: current_dist, u = heapq.heappop(heap) if current_dist != dist[u]: continue for v, weight in graph[u].items(): alt = dist[u] + weight if alt < dist[v]: dist[v] = alt heapq.heappush(heap, (alt, v)) return dist ``` #### Floyd-Warshall 算法处理复杂情况下的优化策略 对于涉及多个源点之间的短路径计算问题,Floyd-Warshall是一个有效的解决方案。然而,在某些特定场景下(比如本题),通过预先给定的信息可以直接定位受影响的部分并加以简化,从而避免不必要的全量遍历操作,达到降低时间复杂度的效果。值得注意的是,在累加过程中应当选用`long long`类型的变量以防止溢出错误的发生[^2]。 #### 单源短路径查询案例解析 考虑到从指定起点出发前往其余各个顶点间的距离需求,此情形适用于单源短路径类别的算法实现方式。具体而言,即是从某固定位置开始测量至其它任意可达地点的距离长度,并针对不可达的情形输出特殊标记值 `-1` 表明无法访问的状态[^3]。 ```cpp #include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f; vector<pair<int,int>> adj[100]; int dis[100]; void spfa(int src){ queue<int> q; vector<bool> vis(100,false); fill(dis,dis+100,INF); dis[src]=0;q.push(src); while(!q.empty()){ int cur=q.front();q.pop(); vis[cur]=false; for(auto& edge : adj[cur]){ int next=edge.first,cost=edge.second; if(dis[next]>dis[cur]+cost){ dis[next]=dis[cur]+cost; if(!vis[next]){ vis[next]=true; q.push(next); } } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值