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. centos使用yum安装mysql

    参考:http://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/ 1.增加yum数据源 (1.1)从http://dev.mysql.com/dow ...

  2. <开心一笑> 前端工程师你们伤不起!

    前端工程师你们伤不起!! 来自: 刻铭 2011-03-11 14:09:53 前端工程师伤不起  老子几年前进了互联网圈!!!!!!!成了前端工程师,名字是不是很拉风,有木有!!!!!!!!  尼玛 ...

  3. win7下的ipython没有的问题

    在笔记本上安装python2.7后,执行python是可以的,但是ipython却不行. 一.问题排查 在网上搜索了看到python与ipython的区别: 例如:ipython有tab补全功能,然后 ...

  4. Beautiful 疑问小记

    一.获取id和class的text() html = urlopen(real_url) bsObj = BeautifulSoup(html) h1 = bsObj.h1.get_text() co ...

  5. mac 显示set a breakpoint in malloc_error_break to debug 终端显示进程已完成怎么办?

    mac 终端显示 ,0x7fff73dbd300) malloc: *** error for object 0x7fce52d15100: pointer being freed was not a ...

  6. 程序设计入门——C语言 第3周编程练习 2 数字特征值(5分)

    2 数字特征值(5分) 题目内容: 对数字求特征值是常用的编码算法,奇偶特征是一种简单的特征值.对于一个整数,从个位开始对每一位数字编号,个位是1号,十位是2号,以此类推.这个整数在第n位上的数字记作 ...

  7. Delphi TTable 组件

    TTable 是 TDataSet 的派生类,它是基于 BDE 数据库引擎的数据集组件,也是一个较简单的数据组件,可以直接从数据库中获取数据表的数据,只需设置连接的数据库属性(Database) 和所 ...

  8. Openfire开发配置,Openfire源代码配置,OpenFire二次开发配置

    原文:http://www.cnblogs.com/lixiaolun/archive/2013/12/07/3462780.html 1.下载源代码:http://www.igniterealtim ...

  9. unity3d Light Probe Group图解超详细使用方法

    原创文章如需转载请注明:转载自http://blog.csdn.net/qq617119142 第一步,创建一个Panel 和 三个 cube,搭建成如下图形状 第二步,创建2个点光源,一个为绿灯,一 ...

  10. informix dbaccess 常用执行方式及常见技巧

    假设: A.数据库servername: testserver B.数据库名:testdb C.SQL脚本文件: sqlfile.sql create table test_table(c1 inte ...