Codeforces715B-Complete The Graph(最短路)
来源:互联网 发布:做快递怎么找淘宝客户 编辑:程序博客网 时间:2024/04/29 16:10
题目链接
http://codeforces.com/problemset/problem/715/B
思路
- 先将所有边长为0的边看做断路,从T做一次最短路,得到g[],代表从T到所有点的最短路
- 从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
- Codeforces715B-Complete The Graph(最短路)
- 【Dijkstra模板】codeforces715B Complete The Graph(最短路径)
- codeforces 716D. Complete The Graph(最短路)
- Codeforces 716D Complete The Graph【思维+最短路】
- CodeForces 716B Complete The Graph(二分搜索)(最短路)
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
- Codeforces Round #372 (Div. 1) D. Complete The Graph(最短路,想法题,好题)
- Codeforces Round #372 (Div. 2) D. Complete The Graph 图论、最短路、Dijkstra、路径、分配部分边权
- Codeforces Round #372 (Div. 2) -- D. Complete The Graph(Dijkstra单源最短路)
- hdu5876 Sparse Graph(最短路)
- hdu5876 Sparse Graph(bfs最短路)
- HDU 4725 The Shortest Path in Nya Graph(图论+最短路)
- HDU 4725 The Shortest Path in Nya Graph(最短路 SPFA 建图)
- HDU 4725 The Shortest Path in Nya Graph (最短路)
- HDU 4725 The Shortest Path in Nya Graph(最短路)
- HDU 4725 The Shortest Path in Nya Graph (最短路拆点建图)
- HDU 4725 The Shortest Path in Nya Graph(拆点+最短路)
- hdu 4725 The Shortest Path in Nya Graph(最短路)
- debug trace 一些资料
- 移动APP集成支付宝--服务器端设计
- 使用MyBatis+MySQL插入记录后返回该记录主键ID
- Swift - CocoaPods的安装使用详解(自动安装管理第三方库)
- string和LPCWSTR的直接转换函数
- Codeforces715B-Complete The Graph(最短路)
- 【Python】求笛卡尔乘积
- oracle 创建表空间
- 基础知识之接口
- 07:取近似值
- Transforming Code into Beautiful,Idiomatic Python(1)
- “Guccifer 2.0”再公布DNC档案,民主党官员怀疑俄罗斯特工
- 1023 组个最小数
- C++ 学习笔记---善良的算法讲解