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(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;
}
posted @ 2019-08-17 01:14  YDDDD  阅读(267)  评论(0编辑  收藏  举报