Sightseeing
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8968   Accepted: 3139

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, BN, AB 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, FN and SF: 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

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

Hint

The first test case above corresponds to the picture in the problem description.

Source

题意:
题解:
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<vector>
#include<string>
#include<stdio.h>
#include<cstring>
#include<iostream>
#include<algorithm>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
#define A first
#define B second
const int mod=;
const int MOD1=;
const int MOD2=;
const double EPS=0.00000001;
typedef __int64 ll;
const ll M22OD=;
const int INF=;
const ll MAX=1ll<<;
const double eps=1e-;
const double inf=~0u>>;
const double pi=acos(-1.0);
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
struct node
{
int v,next,w;
}edge[];
int d[][],e,n,m;
int cnt[][];
int head[];
bool vis[][];
void init()
{
e=;
memset(head,,sizeof(head));
}
void insert(int x,int y,int w)
{
e++;
edge[e].v=y;
edge[e].w=w;
edge[e].next=head[x];
head[x]=e;
}
int dijkstra(int s,int t)
{
int flag,u;
memset(vis,,sizeof(vis));
memset(cnt,,sizeof(cnt));
for(int i=;i<=n;i++){
d[i][]=d[i][]=INF;
}
cnt[s][]=;
d[s][]=;
for(int i=;i<=*n;i++)
{
int mini=INF;
for(int j=;j<=n;j++)
{
if(!vis[j][]&&d[j][]<mini)
{
u=j;
flag=;
mini=d[j][];
}
else if(!vis[j][]&&d[j][]<mini)
{
u=j;
flag=;
mini=d[j][];
}
}
if(mini==INF) break;
vis[u][flag]=;
for(int j=head[u];j;j=edge[j].next)
{
int w=edge[j].w;
int v=edge[j].v;
if(d[v][]>mini+w){
d[v][]=d[v][];
cnt[v][]=cnt[v][];
d[v][]=mini+w;
cnt[v][]=cnt[u][flag];
}
else if(d[v][]==mini+w) cnt[v][]+=cnt[u][flag];
else if(d[v][]>mini+w){
d[v][]=mini+w;
cnt[v][]=cnt[u][flag];
}
else if(d[v][]==mini+w) cnt[v][]+=cnt[u][flag];
}
}
int ans=;
if(d[t][]==d[t][]+) ans=cnt[t][]+cnt[t][];
else ans=cnt[t][];
return ans;
}
int main()
{
int s,t, T,x,y,w;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d %d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&w);
insert(x,y,w);
}
scanf("%d %d",&s,&t);
printf("%d\n",dijkstra(s,t));
}
return ;
}

poj 3463 最短路与次短路的方案数求解的更多相关文章

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

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

  2. poj 3463 Sightseeing( 最短路与次短路)

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  3. poj 3463 Sightseeing——次短路计数

    题目:http://poj.org/problem?id=3463 当然要给一个点记最短路和次短路的长度和方案. 但往优先队列里放的结构体和vis竟然也要区分0/1,就像把一个点拆成两个点了一样. 不 ...

  4. POJ - 3463 Sightseeing 最短路计数+次短路计数

    F - Sightseeing 传送门: POJ - 3463 分析 一句话题意:给你一个有向图,可能有重边,让你求从s到t最短路的条数,如果次短路的长度比最短路的长度多1,那么在加上次短路的条数. ...

  5. POJ 3463 有向图求次短路的长度及其方法数

    题目大意: 希望求出走出最短路的方法总数,如果次短路只比最短路小1,那也是可取的 输出总的方法数 这里n个点,每个点有最短和次短两种长度 这里采取的是dijkstra的思想,相当于我们可以不断找到更新 ...

  6. poj 3463 Sightseeing(次短路+条数统计)

    /* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...

  7. poj 3463 最短路+次短路

    独立写查错不能,就是维护一个次短路的dist 题意:给定一个有向图,问从起点到终点,最短路+比最短路距离长1的路的个数. Sample Input25 81 2 31 3 21 4 52 3 12 5 ...

  8. poj 3463 次短路

    题意:给定一个有向图,问从起点到终点,最短路+比最短路距离长1的路的个数. 当年数据结构课程设计用A*做过,现在忘光了,2333 #include<stdio.h> #include< ...

  9. POJ 3463 Sightseeing (次短路)

    题意:求两点之间最短路的数目加上比最短路长度大1的路径数目 分析:可以转化为求最短路和次短路的问题,如果次短路比最短路大1,那么结果就是最短路数目加上次短路数目,否则就不加. 求解次短路的过程也是基于 ...

随机推荐

  1. phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别

    [题外话]phpinfo()这个函数可以查看很多php的信息,要 记得使用 phpinfo() 的很多部分有两个Column:Local Value(局部变量)和 Master Value(主变量) ...

  2. touch穿透

    出现穿透的前提:上层绑定touch事件,下层绑定click事件. 解决方案:touchend绑定: e.preventDefault();$(下层).css("pointer-events& ...

  3. 小计-git

    今天在发布项目的时候遇到了一个问题,就是项目一直提示与最新版本有差异,导致发布不不成功.仔细考虑了一下,自己这次的开发与原来的是不一样的,父子工程,不是单模块开发,发现原来是别人发布过他们的模块到ma ...

  4. Deep Learning 17:DBN的学习_读论文“A fast learning algorithm for deep belief nets”的总结

    1.论文“A fast learning algorithm for deep belief nets”的“explaining away”现象的解释: 见:Explaining Away的简单理解 ...

  5. Deep Learning 2_深度学习UFLDL教程:矢量化编程(斯坦福大学深度学习教程)

    1前言 本节主要是让人用矢量化编程代替效率比较低的for循环. 在前一节的Sparse Autoencoder练习中已经实现了矢量化编程,所以与前一节的区别只在于本节训练集是用MINIST数据集,而上 ...

  6. [Selenium] 拖拽一个 Component 到 Workspace

    先使Component可见,获取Component位置信息,获取Workspace位置信息,点击Component并拖拽到Workspace,最后释放.(调试时dragAndDropOffset()方 ...

  7. 微软2017校招笔试题3 registration day

    题目 It's H University's Registration Day for new students. There are M offices in H University, numbe ...

  8. canvas初体验之基本线条

    有的时候我们打开一些网站,可以看到背景是闪烁的星空或者是有一些可以与鼠标交互的线条等等,此酷炫的效果就是用到了html5的canvas效果. 首先来认识一下h5新增的标签的写法<canvas&g ...

  9. Clojure学习笔记(一)——介绍、安装和语法

    什么是Clojure Clojure是一种动态的.强类型的.寄居在JVM上的语言. Clojure的特性: 函数式编程基础,包括一套性能可以和典型可变数据结构媲美的持久性数据结构 由JVM提供的成熟的 ...

  10. google应用商店的解决

    { "enabled_plugins": [ "SimpleReloadPlugin", "SimpleRefresh" ] } git c ...