Sightseeing

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions:10005   Accepted: 3523

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 AB and L, separated by single spaces, with 1 ≤ AB ≤ NA ≠ 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 ≤ SF ≤ 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 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.

思路

一开始我想了一下,用A*好像可以做,但是超了内存,看了一下discuss,原因应该是不断入队造成的,这让我感到十分无奈,毕竟刚刚才用a*过了一题。

具体的东西我写在注释里了,注意次短路的入队操作!

#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const ll inf = 210000000000000;
int n,m,s,t;
vector<int>u[1024];
vector<ll>w[1024];
ll dis[1024][2];
//虽然按照我的推理,这个题不会爆int ,可是不用int,就是会wa;
bool book[1024][2];
ll ans[1024][2];
struct node
{
int num;
ll dis;
int flag;
bool operator<(const node x)const
{
return x.dis<dis;
}
}; ll Dijkstra()
{
priority_queue<node>q;
node exa;
dis[s][0]=0;ans[s][0]=1;
q.push(node{s,0,0});
while(!q.empty()){
exa=q.top();q.pop();
int st = exa.num;//当前起点
ll diss=exa.dis;//diss表示,当前节点的最短(或次短)
int f=exa.flag;//表示这是最短还是次短
if(book[st][f]){continue;}
book[st][f]=true;
int siz=u[st].size();
for(int i=0;i<siz;i++){
int y=u[st][i];
ll ww=w[st][i];
/*y是下一个节点,ww是路径*/
if(dis[y][0]>diss+ww){//最短的距离可以更新
q.push(node{y,dis[y][0],1});
dis[y][1]=dis[y][0];//下一节点的次短路,就是更新前的最短路
ans[y][1]=ans[y][0];//下一节点的次短路条数,就是更新前的最短路条数
dis[y][0]=diss+ww;//更新最短路
ans[y][0]=ans[st][0];//下一最短路的条数,就是当前节点最短/次短的条数
//这个位置不可能是次短路更新
q.push(node{y,diss+ww,0});
}
else if(dis[y][0]==diss+ww){//下一节点的最短距离,就是当前距离
ans[y][0]+=ans[st][0];
}
else if(dis[y][1]>diss+ww){//次短路可更新
dis[y][1]=diss+ww;//更新次短路
ans[y][1]=ans[st][f];//下一节点的次短路条数,就是更新来源的路径数
q.push(node{y,diss+ww,1});
}
else if(dis[y][1]==diss+ww){
ans[y][1]+=ans[st][f];//下一节点的路径数,加上当前节点的路径数
}
}
}
if(dis[t][1]==dis[t][0]+1){
return ans[t][0]+ans[t][1];
}
return ans[t][0];
} void init()
{
for(int i=0;i<=n+1;i++){
for(int j=0;j<=1;j++){
dis[i][j]=inf;
}
}
memset(book,0,sizeof(book));
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++){
u[i].clear();
w[i].clear();
}
} int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
int x,y;ll z;
for(int i=1;i<=m;i++){
scanf("%d%d%lld",&x,&y,&z);
u[x].push_back(y);
w[x].push_back(z);
}
scanf("%d%d",&s,&t);
printf("%lld\n",Dijkstra());
}
}

  妈呀,wa死我了,a个题真不容易

POJ 3463 Sightseeing (次短路经数)的更多相关文章

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

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

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

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

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

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

  4. POJ 3463 Sightseeing (次短路)

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

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

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

  6. POJ 3463 Sightseeing

    最短路+次短路(Dijkstra+priority_queue) 题意是要求你找出最短路的条数+与最短路仅仅差1的次短路的条数. 開始仅仅会算最短路的条数,和次短路的长度.真是给次短路条数跪了.ORZ ...

  7. POJ 3463 Sightseeing 【最短路与次短路】

    题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...

  8. POJ 3463 Sightseeing 题解

    题目 Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the ...

  9. POJ 3463 最(次)短路条数

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9497   Accepted: 3340 Descr ...

随机推荐

  1. 多线程的实现方式01 Thread

    /* * 多线程 有三种实现方式 * 其一 Thread * * 写一个类 * * 1.让他继承 Thread * 2.重写thread中的run方法 * 3.创建子类对象就是在 创建线程! * 3. ...

  2. 【python练习题】程序13

    #题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身.例如:153是一个"水仙花数",因为153= ...

  3. C 语言-----字符串和输入输出函数

    在C语言中,没有字符串类型,那它是怎么表示字符串呢? 由于字符串是一系列单个字符的组合,所以它用char 类型的数组来表示字符串,在数组中,一个数组元素存放一个char类型字符. ]; name 变量 ...

  4. codeforces581C

    Developing Skills CodeForces - 581C 你在玩一个游戏.你操作的角色有n个技能,每个技能都有一个等级ai.现在你有k次提升技能的机会(将其中某个技能提升一个等级,可以重 ...

  5. CentOS安装GIt、上传项目到git仓库

    上传项目 登录服务器后安装git yum install git 新建文件夹(仓库) mkdir *.git 初始化仓库 git init --bare *.git 在本地初始化仓库 git init ...

  6. 傻瓜式搭建私人网络硬盘——owncloud安装指南

    百度云这个贱货天天删我资源,我已经忍无可忍了,于是想搭建一个owncloud来放我的里番.使用owncloud不仅安全,而且还可以在线播放,离线下载,功能相当强大. 然而·····网上查了一下,竟然无 ...

  7. [NOIP2017] 逛公园 【最短路】【强连通分量】

    题目分析: 首先考虑无数条的情况.出现这种情况一定是一条合法路径经过了$ 0 $环中的点.那么预先判出$ 0 $环中的点和其与$ 1 $和$ n $的距离.加起来若离最短路径不超过$ k $则输出$ ...

  8. python爬取豆瓣前25个影片内容的正则表达式练习

    通过python正则表达式获取豆瓣top250的第一页的25个影片排名,影片名字,影片连接,导演,主演,上映日期,国家,剧情,评分,评价人数的内容 网页html内容: <ol class=&qu ...

  9. 【XSY1762】染色问题 网络流

    题目描述 给定一张\(n\)个点\(m\)条边的无向图.每个顶点有一个颜色,要么是黑,要么是白.我们想进行一些操作,使得最终每一条边的两个端点都是不同的颜色.每一次操作,你可以将一条边的两个端点交换颜 ...

  10. IDEA 新建 module

    maven项目可以创建多个module,在IDEA中具体操作 1.在已经建好的maven项目上右键 2.新建: 效果如下: 这时在子pom.xml中 <parent> <artifa ...