Sightseeing

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 639    Accepted Submission(s): 249

Problem 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 10^9 = 1,000,000,000.

 
Sample Input
2
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
 
Sample Output
3
2
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2833 2433 2363 2482 2377 
 

题意就是 求最短路径数和比最短路径大1的路径数的和。

和3191几乎一样+ +自己写了一下次短路,结果wa了好多次...不过这种解法效率真的很高!

 //15MS    528K    2372 B    C++
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x3fffffff
#define N 1005
using namespace std;
struct edge{
int v,w;
edge(int a,int b){
v=a;w=b;
}
};
struct node{
int v,w;
int mark;
bool operator < (const node &p) const{
return p.w<w;
//return p.v<v;
}
};
vector<edge>V[N];
int n,m,s,e;
int d[N][];
int dp[N][];
int vis[N][];
void dijkstra(int s)
{
for(int i=;i<=n;i++)
d[i][]=d[i][]=inf;
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
priority_queue<node>Q;
node p,q;
d[s][]=;
dp[s][]=;
p.w=,p.mark=,p.v=s;
Q.push(p);
while(!Q.empty()){
p=Q.top();
Q.pop();
if(vis[p.v][p.mark]) continue;
vis[p.v][p.mark]=;
int m=V[p.v].size();
for(int i=;i<m;i++){
int v=V[p.v][i].v;
int w=V[p.v][i].w;
if(!vis[v][] && d[v][]>p.w+w){
if(d[v][]!=inf){
d[v][]=d[v][];
dp[v][]=dp[v][];
q.v=v,q.mark=,q.w=d[v][];
Q.push(q);
}
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.v=v,q.mark=,q.w=d[v][];
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){
dp[v][]+=dp[p.v][p.mark];
}else if(!vis[v][] && d[v][]>p.w+w){
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.v=v,q.mark=,q.w=d[v][];
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){
dp[v][]+=dp[p.v][p.mark];
}
}
}
}
int main(void)
{
int u,v,w;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
V[u].push_back(edge(v,w));
//V[v].push_back(edge(u,w));
}
scanf("%d%d",&s,&e);
dijkstra(s);
int ans=dp[e][];
//printf("*%d %d %d\n",ans,d[e][1],d[e][2]);
if(d[e][]==d[e][]+) ans+=dp[e][];
printf("%d\n",ans);
}
return ;
}

hdu 1688 Sightseeing (最短路径)的更多相关文章

  1. HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. HDU 1688 Sightseeing

    题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio. ...

  3. HDU 1688 Sightseeing 【输出最短路+次短路条数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...

  4. 【最短路】HDU 1688 Sightseeing

    题目大意 给出一个有向图(可能存在重边),求从\(S\)到\(F\)最短路的条数,如果次短路的长度仅比最短路的长度多1,那么再加上次短路的条数. 输入格式 第一行是数据组数\(T\). 对于魅族数据, ...

  5. HDU ACM 3790 最短路径问题

    最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. poj 3463/hdu 1688 求次短路和最短路个数

    http://poj.org/problem?id=3463 http://acm.hdu.edu.cn/showproblem.php?pid=1688 求出最短路的条数比最短路大1的次短路的条数和 ...

  7. 【HDOJ】1688 Sightseeing

    Dijkstra求解次短路径,使用cnt和dis数组记录最小.次小的个数和长度.重写更新操作. /* 1688 */ #include <iostream> #include <st ...

  8. hdu 3790 (最短路径问题dijkstra)

    主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起 ...

  9. HDU 2112 HDU Today(最短路径+map)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. 天津市人民优步Uber司机奖励政策(9月14日~9月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. 封装Excls数据导出功能 返回一个下载链接地址

    /// <summary> /// 获取本地存储地址 /// </summary> /// <param name="dt"></para ...

  3. POM中常用依赖包

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  4. L011系统文件属性知识进阶详解小节

    L011系统文件属性知识进阶详解小节 这节课的内容相对来说较少,一上午加中午就听完了,现在总结一下,最后会有一个相关的面试题. 首先先附上一张图: 今天学习主要跟①和②有关,①为Inode 号 ②为文 ...

  5. golang 仿python pack/unpack

    写得不完善也不完美 尤其是高低位转换那(go和c 二进制高地位相反 需要转换,还有go int转[]byte长度是4位),希望牛人看后指导一下 项目需要通过socket调取 客户端是go ,服务器端是 ...

  6. 【JUC源码解析】AQS

    简介 AQS,也即AbstractQueuedSynchronizer,抽象队列同步器,提供了一个框架,可以依赖它实现阻塞锁和相关同步器.有两种类型,独占式(Exclusive)和共享式(Share) ...

  7. Linux命令应用大词典-第42章 PostgreSQL数据库

    42.1 initdb:初始化PostgreSQL数据库 42.2 pg_ctl:控制PostgreSQL服务 42.3 psql:PostgreSQL交互式客户端工具 42.4 createdb:创 ...

  8. artDialog使用说明(弹窗API)

    Js代码 2. 传入HTMLElement    备注:1.元素不是复制而是完整移动到对话框中,所以原有的事件与属性都将会保留 2.如果隐藏元素被传入到对话框,会设置display:block属性显示 ...

  9. Leetcode-跳跃游戏

    跳跃游戏     给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 判断你是否能够到达最后一个位置. 示例 1: 输入: [2,3,1,1,4] ...

  10. lintcode172 删除元素

    删除元素   给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个数组 [0, ...