POJ 3463 Sightseeing 次短路
2016-03-03 17:33
281 查看
求最短路和次短路(如果长度刚好比最短路多1)条数和。
嗯。。Dij写成dp。。
Total Submissions: 8106 Accepted: 2883
Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.
There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.
For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.
Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.
One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.
M lines, each with three integers A, B and L, separated by single spaces, with 1 ≤ A, B ≤ N, A ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.
The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.
One line with two integers S and F, separated by a single space, with 1 ≤ S, F ≤ N and S ≠ F: the starting city and the final city of the route.
There will be at least one route from S to F.
5 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1
2
嗯。。Dij写成dp。。
#include <cstdio> #include <cstring> #include <queue> #define FOR(i,j,k) for(i=j;i<=k;i++) using namespace std; const int N = 100001, M = 100001; int h , p[M], v[M], w[M], cnt, vis [2], n; struct Data { int x, c, k; }; bool operator <(Data a, Data b){ return a.c > b.c; } struct Maintainer { int d[2], w[2]; int update(int x, int y) { if (x < d[0]) { w[1] = w[0], d[1] = d[0], d[0] = x, w[0] = y; return 0; } else if (x == d[0]) w[0] += y; else if (x < d[1]) { d[1] = x, w[1] = y; return 1; } else if (x == d[1]) { w[1] += y; return -1; } } } d ; void add(int x, int y, int z) { p[++cnt] = h[x]; w[cnt] = z; v[cnt] = y; h[x] = cnt; } int dij(int s, int t) { int i; priority_queue<Data> q; FOR(i,1,n) d[i].d[0] = d[i].d[1] = 0x7f7f7f7f, d[i].w[0] = d[i].w[1] = 0; memset(vis, 0, sizeof vis); d[s].d[0] = 0; d[s].w[0] = 1; q.push((Data) {s, 0, 0}); while (!q.empty()) { Data u = q.top(); q.pop(); if (vis[u.x][u.k]) continue; vis[u.x][u.k] = 1; for (int i = h[u.x]; i; i = p[i]) { int ret = d[v[i]].update(d[u.x].d[u.k] + w[i], d[u.x].w[u.k]); if (ret != -1) q.push((Data) {v[i], d[u.x].w[ret], ret}); if (ret == 0) q.push((Data) {v[i], d[v[i]].d[1], 1}); } } int ret = d[t].w[0]; if (d[t].d[1] == d[t].d[0] + 1) ret += d[t].w[1]; return ret; } int main() { int n, m, t, a, b, c; scanf("%d", &t); while (t--) { cnt = 0; memset(h, 0, sizeof h); scanf("%d%d", &n, &m); while (m--) { scanf("%d%d%d", &a, &b, &c); add(a, b, c); } scanf("%d%d", &a, &b); printf("%d\n", dij(a, b)); } return 0; }
Sightseeing
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8106 Accepted: 2883
Description
Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. On this way, the tourists in the bus can see the sights alongside the route travelled. Moreover, the bus makes a number of stops (zero or more) at some beautiful cities, where the tourists get out to see the local sights.Different groups of tourists may have different preferences for the sights they want to see, and thus for the route to be taken from S to F. Therefore, Your Personal Holiday wants to offer its clients a choice from many different routes. As hotels have been booked in advance, the starting city S and the final city F, though, are fixed. Two routes from S to F are considered different if there is at least one road from a city A to a city B which is part of one route, but not of the other route.
There is a restriction on the routes that the tourists may choose from. To leave enough time for the sightseeing at the stops (and to avoid using too much fuel), the bus has to take a short route from S to F. It has to be either a route with minimal distance, or a route which is one distance unit longer than the minimal distance. Indeed, by allowing routes that are one distance unit longer, the tourists may have more choice than by restricting them to exactly the minimal routes. This enhances the impression of a personal holiday.
For example, for the above road map, there are two minimal routes from S = 1 to F = 5: 1 → 2 → 5 and 1 → 3 → 5, both of length 6. There is one route that is one distance unit longer: 1 → 3 → 4 → 5, of length 7.
Now, given a (partial) road map of the Benelux and two cities S and F, tour operator Your Personal Holiday likes to know how many different routes it can offer to its clients, under the above restriction on the route length.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:One line with two integers N and M, separated by a single space, with 2 ≤ N ≤ 1,000 and 1 ≤ M ≤ 10, 000: the number of cities and the number of roads in the road map.
M lines, each with three integers A, B and L, separated by single spaces, with 1 ≤ A, B ≤ N, A ≠ B and 1 ≤ L ≤ 1,000, describing a road from city A to city B with length L.
The roads are unidirectional. Hence, if there is a road from A to B, then there is not necessarily also a road from B to A. There may be different roads from a city A to a city B.
One line with two integers S and F, separated by a single space, with 1 ≤ S, F ≤ N and S ≠ F: the starting city and the final city of the route.
There will be at least one route from S to F.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of routes of minimal length or one distance unit longer. Test cases are such, that this number is at most 109 = 1,000,000,000.Sample Input
25 8
1 2 3
1 3 2
1 4 5
2 3 1
2 5 3
3 4 2
3 5 4
4 5 3
1 5
5 6
2 3 1
3 2 1
3 1 10
4 5 2
5 2 7
5 2 7
4 1
Sample Output
32
相关文章推荐
- Animation特效——Property Animation
- 实现4.4以上的状态栏变色
- android学习之LayoutParams
- 《Unix环境高级编程》学习笔记:从点到面总结
- 算法:二叉树的先序遍历、中序遍历、后序遍历(递归及非递归方式)的java代码实现
- java泛型
- Java 入门 之 多线程的 start 和 run 方法
- Leetcode ☞ 328. Odd Even Linked List ☆
- 百度地图
- iOS开发中app之间的跳转
- iOS之多线程开发(NSThread,NSOperation,GCD)
- 开篇
- 对象key值为数字时的处理
- php web 信息采集
- Nginx+CI出现404错误
- java 修改字体大小
- Asp.Net MVC Views页面不包含“GetEnumerator”的公共定义
- 有序链表
- android标题栏RadioButton实现,下面展示切换viewpager,fragment,内容是listview
- LTE的关键任务网