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. HttpClient&Jsoup爬虫的简单应用

    详细的介绍已经有很多前辈总结,引用一下该篇文章:https://blog.csdn.net/zhuwukai/article/details/78644484 下面是一个代码的示例: package ...

  2. Java: 数组、列表和集合的互相转换

    1. Array 转 List String[] city = {"Nanjing","Shanghai","Beijing"}; List ...

  3. What is the reason that a likelihood function is not a pdf?

    From: http://stats.stackexchange.com/questions/31238/what-is-the-reason-that-a-likelihood-function-i ...

  4. Ruby基础教程 1-10

    类结构 1.数值类结构     Fixnum到Bignum会自动转换   2.常用数值表示   3. ans=10.divmod(3) ans[0]是商  ans[1]是余数   4.实例方法roun ...

  5. 三年同行,质造未来,腾讯WeTest五大服务免费体验

    WeTest 导读 2018年10月26日,腾讯WeTest将正式迎来三周岁生日.三周年庆典期间,只要在WeTest平台注册的用户,均可免费体验标准兼容.云真机.压测大师.手游安全扫描.应用安全扫描等 ...

  6. 惊喜Skr人,Istio的创始人Shriram Rajagopalan手把手教你如何使用Istio

    Shriram与来自Google.Lyft.IBM和其他公司的社区贡献者们一起并肩作战,积极地向Istio和Envoy项目作贡献.同时,Shriram是IBM的Amalgam8项目的创始成员之一.目前 ...

  7. Allure--自动化测试报告生成

    之前尝试使用过testNG自带的测试报告.优化过reportNG的测试报告,对这两个报告都不能满意.后经查找资料,发现有个神器: Allure(已经有allure2了,笔者使用的就是allure2), ...

  8. MySQL☞substr函数

    substr函数:截取字符串 格式如下: select  substr(参数1,参数2,参数3)  from  表名 参数1:列名/字符串 参数2:起始位置,如果为正数,就表示从正数的位置往下截取字符 ...

  9. [Clr via C#读书笔记]Cp18 定制Attribute

    Cp18 定制Attribute 意义 利用Attribute,可以声明性的给自己的代码结构创建注解,从而实现一些特殊的功能:最终在元数据中生成,这种可扩展的元数据信息可以在运行时的时候查询,从而动态 ...

  10. jupyter notebook 使用cmd命令窗口打开

    第一步:将文件路径改为你需要使用文件所在的路径 第二部:   jupyter notebook