CodeForces-449B (single-source shortest, thinking)

link:

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

Meaning of the questions:

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.

Ideas:

First seek the shortest path to each point, while the shortest recorded at each point there are several.
Finally compare each train track, it can be deleted.

Code:

#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(HeapNode{1, 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(HeapNode{ne, 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(Edge{v, w});
        G[v].push_back(Edge{u, w});
    }
    for (int i = 1;i <= k;i++)
    {
        cin >> v >> w;
        To[i] = v, Va[i] = w;
        G[1].push_back(Edge{v, 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;
}

Guess you like

Origin www.cnblogs.com/YDDDD/p/11367121.html