Codeforces715B-Complete The Graph(最短路)

来源:互联网 发布:做快递怎么找淘宝客户 编辑:程序博客网 时间:2024/04/29 16:10

题目链接

http://codeforces.com/problemset/problem/715/B

思路

  1. 先将所有边长为0的边看做断路,从T做一次最短路,得到g[],代表从T到所有点的最短路
  2. 从S开始做最短路,结果保存在d[]中,当遇到边长为0的边,设置其长度为L - g[v] - d[u],小于1的话设置为1

代码

#include <iostream>#include <cstring>#include <stack>#include <vector>#include <set>#include <map>#include <cmath>#include <queue>#include <sstream>#include <iomanip>#include <fstream>#include <cstdio>#include <cstdlib>#include <climits>#include <deque>#include <bitset>#include <algorithm>using namespace std;#define PI acos(-1.0)#define LL long long#define PII pair<int, int>#define PLL pair<LL, LL>#define mp make_pair#define IN freopen("in.txt", "r", stdin)#define OUT freopen("out.txt", "wb", stdout)#define scan(x) scanf("%d", &x)#define scan2(x, y) scanf("%d%d", &x, &y)#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)#define sqr(x) (x) * (x)#define pr(x) cout << #x << " = " << x << endl#define lc o << 1#define rc o << 1 | 1#define pl() cout << endl#define INF 1e16;const int maxn = 1005;int n, m, S, T;LL L, tot = 0;LL f[maxn], g[maxn];struct Edge {    int from, to;    LL dist;    bool flag;    Edge(int u, int v, LL w, bool ff) : from(u), to(v), dist(w), flag(ff){    }};struct cmp {    bool operator() (PII x, PII y) {        return y.second < x.second;    }};struct Dijkstra {    int n, m;    vector<Edge> edges;    vector<int> G[maxn];    bool vis[maxn];    LL d[maxn];    void init(int n) {        this->n = n;        for (int i = 0; i < n; i++) G[i].clear();        edges.clear();    }    void addedge(int u, int v, int w, bool f) {        edges.push_back(Edge(u, v, w, f));        int m = edges.size();        G[u].push_back(m - 1);    }    LL dijkstra(int s, int t, LL *d) {        priority_queue<PII, vector<PII>, cmp> q;        for (int i = 0; i < n; i++) d[i] = INF;        d[s] = 0;        memset(vis, 0, sizeof(vis));        q.push(mp(s, 0));        while (!q.empty()) {            PII t = q.top(); q.pop();            int u = t.first;            if (vis[u]) continue;            vis[u] = true;            int _s = G[u].size();            for (int i = 0; i < _s; i++) {                Edge &e = edges[G[u][i]];                if (d[e.to] > d[u] + e.dist) {                    d[e.to] = d[u] + e.dist;                    q.push(mp(e.to, d[e.to]));                }            }        }        return d[t];    }    LL rejudge(int s, int t) {        priority_queue<PII, vector<PII>, cmp> q;        for (int i = 0; i < n; i++) d[i] = INF;        d[s] = 0;        memset(vis, 0, sizeof(vis));        q.push(mp(s, 0));        while (!q.empty()) {            PII t = q.top(); q.pop();            int u = t.first;            for (int i = 0; i < G[u].size(); i++) {                Edge &e = edges[G[u][i]];                if (e.flag) {                    e.dist = L - g[e.to] - d[e.from];                    if (e.dist < 1) e.dist = 1;                    if (e.dist > 1e18) e.dist = 1e18;                    e.flag = false;                }                if (e.dist < 1e18 && d[e.to] > d[u] + e.dist) {                    d[e.to] = d[u] + e.dist;                    q.push(mp(e.to, d[e.to]));                }            }        }        return d[t];    }};int main() {    scanf("%d%d%I64d%d%d", &n, &m, &L, &S, &T);    Dijkstra D1, D2;    D1.init(n); D2.init(n);    tot = 0;    for (int i = 0; i < m; i++) {        int x, y;        LL z;        scanf("%d%d%I64d", &x, &y, &z);        if (z == 0) {            tot++;            D1.addedge(x, y, 0, 1);            D1.addedge(y, x, 0, 1);        } else {            D1.addedge(x, y, z, 0);            D1.addedge(y, x, z, 0);            D2.addedge(x, y, z, 0);            D2.addedge(y, x, z, 0);        }    }    LL dist2 = D2.dijkstra(T, S, g);    if (dist2 < L) {        puts("NO");        return 0;    }    LL dist = D1.rejudge(S, T);    if (dist > L) puts("NO");    else {        puts("YES");        for (int i = 0; i < D1.edges.size(); i += 2) {            Edge e = D1.edges[i];            printf("%d %d %I64d\n", e.from, e.to, max(e.dist, D1.edges[i + 1].dist));        }    }    return 0;}
0 0
原创粉丝点击