CodeForces-449B(单源最短路,思维)

Posted ydddd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces-449B(单源最短路,思维)相关的知识,希望对你有一定的参考价值。

链接:

https://vjudge.net/problem/CodeForces-449B

题意:

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.

思路:

先求到每个点的最短路,同时记录到每个点的最短路有几条.
最后比较每条火车道,是否可以删除.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;

const int MAXN = 1e6+10;
const long long INF = 1e15;

struct Edge

    int to;
    LL v;
;

struct HeapNode

    int to;
    LL dis;
    bool  operator < (const HeapNode& that) const
    
        return this->dis > that.dis;
    
;

vector<Edge> G[MAXN];
int In[MAXN], Vis[MAXN];
LL Dis[MAXN];
int To[MAXN], Va[MAXN];
int n, m, k;

void Dij()

    memset(Vis, 0, sizeof(Vis));
    memset(In, 0, sizeof(In));
    In[1] = 1;
    for (int i = 1;i <= n;i++)
        Dis[i] = INF;
    Dis[1] = 0;
    priority_queue<HeapNode> que;
    que.push(HeapNode1, 0LL);
    while (!que.empty())
    
        HeapNode node = que.top();
        que.pop();
        if (Vis[node.to])
            continue;
        Vis[node.to] = 1;
        for (int i = 0;i < G[node.to].size();i++)
        
            int ne = G[node.to][i].to;
            if (Vis[ne])
                continue;
            LL va = G[node.to][i].v;
            if (Dis[ne] == Dis[node.to]+va)
                In[ne]++;
            if (Dis[ne] > Dis[node.to]+va)
            
                In[ne]=1;
                Dis[ne] = Dis[node.to]+va;
            
            que.push(HeapNodene, Dis[ne]);
        
    


int main()

    ios::sync_with_stdio(false);
    cin.tie(0);
    int u, v, w;
    cin >> n >> m >> k;
    for (int i = 1;i <= m;i++)
    
        cin >> u >> v >> w;
        G[u].push_back(Edgev, w);
        G[v].push_back(Edgeu, w);
    
    for (int i = 1;i <= k;i++)
    
        cin >> v >> w;
        To[i] = v, Va[i] = w;
        G[1].push_back(Edgev, w);
    
    Dij();
//    for (int i = 1;i <= n;i++)
//        cout << Dis[i] << ' ' ;
//    cout << endl;
    int num = 0;
    for (int i = 1;i <= k;i++)
    
        if (Dis[To[i]] < Va[i])
            num++;
        else if (Dis[To[i]] == Va[i] && In[To[i]] > 1)
            num++, In[To[i]]--;
    
    cout << num << endl;

    return 0;

以上是关于CodeForces-449B(单源最短路,思维)的主要内容,如果未能解决你的问题,请参考以下文章

POJ-1511 Invitation Cards (单源最短路+逆向)

[模板]单源最短路径

❤️数据结构入门❤️(3 - 5)- 单源最短路

单源最短路径小结

求大佬用java帮我实现dijkstra算法,单源最短路径

Dijkstra算法求单源最短路径