Codeforces 449b jzzhu and cities (Shortest Path)

Source: Internet
Author: User

Link: codeforces 449b jzzhu and cities

Jzzhu is the president of a country. There are n cities in this country, with 1 as the capital. m roads already exist and M roads are given. There are also K railroad tracks with the capital and Si distance being Yi. Now, jzzhu wants to save money and dismantle some rails. He asks how many rails can be demolished at most, and the shortest distance between each city and the capital remains unchanged.

Solution: the shortest path: add an array of tags, and mark 1 for each Si. If the shortest path of these points is updated, the mark of the corresponding Si is cleared to 0, finally, count the remaining marks, that is, tracks that cannot be removed.

#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>using namespace std;typedef long long ll;typedef pair<int, int> pii;const int maxn = 1e5 + 5;const ll INF = 0x3f3f3f3f3f3f3f3f;int N, M, K, p[maxn], vis[maxn];ll d[maxn];vector<pii> g[maxn];void init () {    scanf("%d%d%d", &N, &M, &K);    int u, v, x;    for (int i = 0; i < M; i++) {        scanf("%d%d%d", &u, &v, &x);        g[u].push_back(make_pair(v, x));        g[v].push_back(make_pair(u, x));    }}int solve () {    for (int i = 0; i <= N; i++)        d[i] = INF;    d[1] = 0;    memset(p, 0, sizeof(p));    queue<int> que;    vis[1] = 1;    que.push(1);    for (int i = 1; i <= K; i++) {        int u, x;        scanf("%d%d", &u, &x);        if (d[u] > x) {            d[u] = x; p[u] = 1;            if (vis[u] == 0) {                vis[u] = 1;                que.push(u);            }        }    }    while (!que.empty()) {        int u = que.front();        que.pop();        vis[u] = 0;        for (int i = 0; i < g[u].size(); i++) {            int v = g[u][i].first, x = g[u][i].second;            if (d[v] >= d[u] + x && p[v])                p[v] = 0;            if (d[v] > d[u] + x) {                d[v] = d[u] + x;                if (vis[v] == 0) {                    vis[v] = 1;                    que.push(v);                }            }        }    }    for (int i = 1; i <= N; i++)        K -= p[i];    return K;}int main () {    init();    printf("%d\n", solve());    return 0;}

Codeforces 449b jzzhu and cities (Shortest Path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.