Codeforces 449B Jzzhu and Cities【最短路SPFA+思维+玄学优先队列】

探讨在一个包含多个城市、道路与特殊火车路线的网络中,如何通过计算最短路径来确定哪些火车路线可以安全移除而不影响各城市到首都的距离。

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 mroads 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 ≤ nui ≠ 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个点,m条无向边,k条火车路。

火车路是从节点1直接通向节点的路。

问最多可以拆除多少条火车路。


思路:

1、首先,建图之后跑最短路 ,得到从节点1到其他个点的最短路。


2、然后将dis【i】<火车路花费的火车路拆除掉,因为显然这条火车路是不需要的,因为有更短的路径比火车路的路径更短


3、当dis【i】==火车路花费的时候,我们要记录节点i的最短路径入度,如果入度>1,那么就可以拆除当前火车路。因为这样就说明有两条及以上的路径能够从1到达节点i,那么这个火车路也是多余的。那么要如何记录节点i的最短路径入度呢?我们在松弛的时候加上两个语句:

if(dis【v】>dis【u】+w)in【v】=1;

if(dis【v】==dis【u】+w)in【v】++;

这样我们就能够统计下来节点i的最短路径条数。


4、玄学优先队列。我写的是SPFA,队列TLE on 45,栈TLE on 4,因为比较渣,堆写的很挫,写了发优先队列+Dij,也是TLE on45,最后百度了一下题解,竟然可以用优先队列水过数据.........................(难道优先队列真的能够优化算法?)


Ac代码;


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
ll head[1000000];
struct node
{
    ll from;
    ll to;
    ll next;
    ll w;
}e[1515151];
ll in[15151515];
ll go[1515151];
ll val[1515151];
ll dis[1515155];
ll vis[1515155];
ll n,m,k,cont;
void add(ll from,ll to,ll w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void SPFA(ll ss)
{
    priority_queue<ll>s;
    for(ll i=1;i<=n;i++)dis[i]=1000000000000000000;
    memset(vis,0,sizeof(vis));
    vis[ss]=1;
    dis[ss]=0;
    s.push(ss);
    while(!s.empty())
    {
        ll u=s.top();
        s.pop();
        vis[u]=0;
        for(ll i=head[u];i!=-1;i=e[i].next)
        {
            ll v=e[i].to;
            ll w=e[i].w;
            if(dis[v]>dis[u]+w)
            {
                in[v]=1;
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
            else if(dis[v]==dis[u]+w)in[v]++;
        }
    }
}
int main()
{
    while(~scanf("%I64d%I64d%I64d",&n,&m,&k))
    {
        cont=0;
        memset(in,0,sizeof(in));
        memset(head,-1,sizeof(head));
        for(ll i=0;i<m;i++)
        {
            ll x,y,w;
            scanf("%I64d%I64d%I64d",&x,&y,&w);
            add(x,y,w);
            add(y,x,w);
        }
        for(ll i=0;i<k;i++)
        {
            scanf("%I64d%I64d",&go[i],&val[i]);
            add(1,go[i],val[i]);
            add(go[i],1,val[i]);
        }
        ll output=0;
        SPFA(1);
        for(ll i=0;i<k;i++)
        {
            if(dis[go[i]]<val[i])output++;
            else if(dis[go[i]]==val[i])
            {
                if(in[go[i]]>1)
                {
                    in[go[i]]--;
                    output++;
                }
            }
        }
        printf("%I64d\n",output);
    }
}





### 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); } } } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值