lightoj 1281 - New Traffic System (有限制最短路)
来源:互联网 发布:科胜通软件 编辑:程序博客网 时间:2024/05/19 18:17
题意 : 给你一张有向图, 然后在给你k条新的路和一个d,问你在建立不超过d条新路条件下节点0 到节点n - 1 的最短路。
思路 : 这道题目可以转换成有限制的最短路来做的,只要把新边认为w = 1,而旧边就是w = 0,最后w总和不超过 d后就是裸裸的有限制最短路,至于有限制的最短路可以用A* 优化做的,评估函数 是f(x) = g(x) + h(x) 其中g(x)表示的是当前搜索到的满足条件的路径长度,而h(x)表示 x 到节点n - 1的最短路径用Dijkstra建立反向边预处理出来,评估函数满足评估值小于等于实际值这一条件。
#include <queue>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int INF = 83178122;const int maxn = 100005;struct AS{ int g, h, x, num; AS(){} AS(int a, int b, int c, int d) : g(a), h(b), x(c), num(d){} bool operator < (const AS &cmp)const { return g + h > cmp.g + cmp.h; }};struct Dist{ int d, x; Dist(){} Dist(int a, int b) : d(a), x(b){} bool operator < (const Dist &cmp)const { return d > cmp.d; }};struct Edge{ int to, c, w, next;}edge[2][maxn<<1];int head[2][maxn], E[2];int n, m, k, Lim;int vis[maxn], d[maxn];void init(){ E[0] = E[1] = 0; memset(head, -1, sizeof(head));}void add_edge(int op, int u, int to, int c, int w){ edge[op][E[op]].to = to; edge[op][E[op]].w = w; edge[op][E[op]].c = c; edge[op][E[op]].next = head[op][u]; head[op][u] = E[op]++;}void Dijkstra(){ fill(d, d+n, INF); d[n-1] = 0; memset(vis, 0, sizeof(vis)); priority_queue<Dist> q; q.push(Dist(d[n-1], n-1)); while (!q.empty()){ Dist tmp = q.top(); q.pop(); int u = tmp.x; if (vis[u])continue; vis[u] = 1; for (int i = head[1][u]; i != -1; i = edge[1][i].next){ int to = edge[1][i].to, c = edge[1][i].c; if (d[to] > d[u] + c){ d[to] = d[u] + c; q.push(Dist(d[to], to)); } } } return ;}int A_star(){ Dijkstra(); priority_queue<AS> q; q.push(AS(0, d[0], 0, 0)); int ret = INF; while (!q.empty()){ AS tmp = q.top(); q.pop(); int u = tmp.x; if (tmp.num > Lim)continue; if (u == n - 1)return tmp.g; for (int i = head[0][u]; i != -1; i = edge[0][i].next){ int to = edge[0][i].to, c = edge[0][i].c, w = edge[0][i].w; q.push(AS(tmp.g+c, d[to], to, tmp.num+w)); } } return ret;}int main(){ int T; scanf("%d", &T); for (int cas = 1; cas<= T; cas++){ scanf("%d%d%d%d", &n, &m, &k, &Lim); init(); for (int i = 1; i <= m; i++){ int a, b, c; scanf("%d%d%d", &a, &b, &c); add_edge(0, a, b, c, 0); add_edge(1, b, a, c, 0); } for (int i = 1; i <= k; i++){ int a, b, c; scanf("%d%d%d", &a, &b, &c); add_edge(0, a, b, c, 1); add_edge(1, b, a, c, 1); } int ret = A_star(); if (ret >= INF)printf("Case %d: Impossible\n", cas); else printf("Case %d: %d\n", cas, ret); } return 0;}
- lightoj 1281 - New Traffic System (有限制最短路)
- LightOJ - 1281 New Traffic System(二维最短路)
- LightOJ - 1281 New Traffic System
- lightoj 1281 - New Traffic System
- light oj 1281 - New Traffic System (最短路+dp思想)
- [最短路] HDU 5910 Advanced Traffic System
- SGU 103. Traffic Lights 带限制最短路
- 有限制最短路hdu4179
- LightOJ 1210 Efficient Traffic System
- lightoj 1210 - Efficient Traffic System
- hdu4396 spfa有限制的最短路
- poj 1062(有限制的最短路)
- poj 1724(有限制的最短路)
- hdu4885 有 限制的最短路
- POJ1062(有限制的最短路)
- LightOJ - 1210 Efficient Traffic System(强连通)
- Lightoj 1281 (二维最短路)
- 【HDU】5910 Advanced Traffic System【线段树+并查集】【数据结构模拟最短路】
- Android 运行 C/C++程序 (使用 arm-linux-gcc 交叉编译环境)
- (Relax ST1.29)POJ 3672 Long Distance Racing(来回路问题..)
- TreeMap使用案例
- opencv形态学处理
- Flex4快速开始
- lightoj 1281 - New Traffic System (有限制最短路)
- 开源SVM库libSVM介绍
- C++的String类型
- Nuttx 移植到 Renesas RX62N
- 上采样与插值滤波器
- libSVM介绍(二)
- 2013.11.26
- Java线程的两种创建方式及优劣比较
- poj 1050 To the Max_dp求最大子矩阵和