Lightoj 1281 (二维最短路)
来源:互联网 发布:电子网络发票系统下载 编辑:程序博客网 时间:2024/06/05 09:25
题解
给你N个点,M条已有的边,K条可以添加的边,K条中最多只能选择D条,问0到N-1的最短路,多开一维记录走到该点选择了多少条路即可
代码
#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int N = 10010;const int M = 30010;const int INF = 0x3f3f3f3f;struct Edge{ int u, v, mark, dis, next; Edge() {} Edge(int u, int v, int mark, int dis, int next): u(u), v(v), mark(mark), dis(dis), next(next){}}E[M];struct Node { int u, dis, times; Node() {} Node(int u, int dis, int times): u(u), dis(dis), times(times) {} bool operator < (const Node &a) const { return dis > a.dis; }};int head[N], d[N][12];int n, m, k, t, cas = 1;bool vis[N][12];void init() { memset(head, -1, sizeof(head)); scanf("%d%d%d%d", &n, &m, &k, &t); int u, v, dis; for (int i = 0; i < m; i++) { scanf("%d%d%d", &u, &v, &dis); E[i] = Edge(u, v, 0, dis, head[u]); head[u] = i; } for (int i = 0; i < k; i++) { scanf("%d%d%d", &u, &v, &dis); E[i + m] = Edge(u, v, 1, dis, head[u]); head[u] = i + m; }}void solve() { priority_queue<Node> Q; memset(d, 0x3f, sizeof(d)); memset(vis, 0 ,sizeof(vis)); d[0][0] = 0; Q.push(Node(0, 0, 0)); while (!Q.empty()) { int u = Q.top().u; int dis = Q.top().dis; int times = Q.top().times; Q.pop(); if (vis[u][times]) continue; vis[u][times] = true; for (int i = head[u]; ~i; i = E[i].next) { int v = E[i].v; if (times + E[i].mark > t) continue; if (d[v][times + E[i].mark] > d[u][times] + E[i].dis) { d[v][times + E[i].mark] = d[u][times] + E[i].dis; Q.push(Node(v, d[v][times + E[i].mark], times + E[i].mark)); } } } int ans = INF; for (int i = 0; i <= t; i++) ans = min(d[n - 1][i], ans); if (ans == INF) printf("Case %d: Impossible\n", cas++); else printf("Case %d: %d\n", cas++, ans);}int main() { int test; scanf("%d", &test); while (test--) { init(); solve(); } return 0;}
题目
The country - Ajobdesh has a lot of problems in traffic system. As the Govt. is very clever (!), they made a plan to use only one way roads. Two cities s and t are the two most important cities in the country and mostly people travel from s to t. That’s why the Govt. made a new plan to introduce some new one way roads in the traffic system such that the time to travel from s to t is reduced.
But since their budget is short, they can’t construct more than d roads. So, they want to construct at most d new roads such that it becomes possible to reach t from s in shorter time. Unluckily you are one living in the country and you are assigned this task. That means you will be given the existing roads and the proposed new roads, you have to find the best path from s to t, which may allow at most d newly proposed roads.
Input
Input starts with an integer T (≤ 30), denoting the number of test cases.
Each case starts with a line containing four integers n (2 ≤ n ≤ 10000), m (0 ≤ m ≤ 20000), k (0 ≤ k ≤ 10000), d (0 ≤ d ≤ 10) where n denotes the number of cities, m denotes the number of existing roads and k denotes the number of proposed new roads. The cities are numbered from 0 to n-1 and city 0 is denoted as s and city (n-1) is denoted as t.
Each of the next m lines contains a description of a road, which contains three integers ui vi wi (0 ≤ ui, vi < n, ui ≠ vi, 1 ≤ wi ≤ 1000) meaning that there is a road from ui to vi and it takes wi minutes to travel in the road. There is at most one road from one city to another city.
Each of the next k lines contains a proposed new road with three integers ui vi wi (0 ≤ ui, vi < n, ui ≠ vi 1 ≤ wi ≤ 1000) meaning that the road will be from ui to vi and it will take wi minutes to travel in the road. There can be at most one proposed road from one city to another city.
Output
For each case, print the case number and the shortest path cost from s to t or “Impossible” if there is no path from s to t.
Sample Input
2
4 2 2 2
0 1 10
1 3 20
0 2 5
2 3 14
2 0 1 0
0 1 100
Sample Output
Case 1: 19
Case 2: Impossible
- Lightoj 1281 (二维最短路)
- LightOJ - 1281 New Traffic System(二维最短路)
- LightOJ 1002(最短路Dijkstra算法)
- hdu 4396(spfs/二维最短路)
- 二维最短路
- lightoj 1174 - Commandos(最短路)
- lightoj 1281 - New Traffic System (有限制最短路)
- LightOJ 1221 Travel Company(贝尔曼最短路判负环)
- lightoj 1002 - Country Roads(最短路变形)
- 最短路题目整理 Hdu 2066 + 1839 + LightOJ 1099(次短路)
- [最短路+水题]LightOJ 1019 - Brush (V)
- 【最短路】 LightOJ 1002 - Country Roads 变形
- LIGHTOJ 1019 – BRUSH (V) 【最短路】
- LightOJ Brush (V) 1019【简单最短路】
- lightoj 1019 - Brush (V) 【最短路 floyd】
- LightOJ - 1019 Brush (V) (最短路) 水
- LightOJ 1002 Country Roads 最短路变形
- LightOJ-1099-spaf,次最短路
- linux命令别名与常用快捷键-52
- 路由映射外网
- c#简单的序列化和反序列化
- 多维数组
- JQuery删除cookie失效
- Lightoj 1281 (二维最短路)
- 我的日记(1)
- @ResponseBody-------springMVC
- UILabel设置富文本格式显示
- 排序算法之直接插入排序
- SlidingMenu源码分析-附自定义底部滑出菜单控件
- gstreamer插件之 v4l2src
- MTF
- FMDB 数据库的基本写法