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. firefox阅读模式

    并不是所有的网页都可以转换为阅读模式的,所以遇到一些识别不了的网页时,可以在地址栏输入“about:reader?url=网址”后回车即可.

  2. 用HttpSessionListener与HttpSessionBindingListener实现在线人数统计

    在线人数统计方面的实现,最初我的想法是,管理session,如果session销毁了就减少,如果登陆用户了就新增一个,但是如果是用户非法退出,如:未注销,关闭浏览器等,这个用户的session是管理不 ...

  3. 深入浅出设计模式——状态模式(State Pattern)

    模式动机 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做状态,这样的对象叫做有状态的 (stateful)对象,这样的对象状态是从事先定义好的一系列值中取出的.当一个这样的 ...

  4. Android 签名工具 shell脚本

    signApk.sh #!/bin/bash #$1 signed and unaligned apk #$2 unsigned apk #$3 aligned apk #./signApk.sh t ...

  5. Java文件操作与输入输出流

    文件操作 package ch15; import java.io.*; /** * Created by Jiqing on 2016/12/28. */ public class FileTest ...

  6. Shell 语法之结构化命令(流程控制)

    许多程序在脚本命令之间需要某种逻辑流控制,允许脚本根据变量值的条件或者其他命令的结果路过一些命令或者循环执行这些命令.这些命令通常被称为结构化命令.和其他高级程序设计语言一样,shell提供了用来控制 ...

  7. Calendar.get()方法--- WEEK_OF_YEAR 、MONTH、

    1. WEEK_OF_YEAR   一年中的第几周 由于西方的一周指的是:星期日-星期六,星期日是一周的第一天,星期六是一周的最后一天, 所以,使用 calendar.get(Calendar.WEE ...

  8. C语言->实验室->指针数组

    一 分析 讨论指针数组要从三个层面来考虑: 1)指针数组本身是什么 2)指针数组作为参数时的表现 3)指针数组作为返回值时的表现 二 指针数组是什么 1)指针数组--指针的集合 数组是若干元素的集合, ...

  9. U-Boot

    U-Boot U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.从FADSROM.8xxROM.PPCBOOT逐步发展演化而来.其源码目录.编译形式与Li ...

  10. 并发编程 19—— 显式的Conditon 对象

    Java并发编程实践 目录 并发编程 01—— ThreadLocal 并发编程 02—— ConcurrentHashMap 并发编程 03—— 阻塞队列和生产者-消费者模式 并发编程 04—— 闭 ...