Sightseeing

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1023    Accepted Submission(s): 444

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
 

题目链接:HDU 1688

后一题原理一模一样,就不放题面了,题目要分别求最短路和次短路两者的条数与长度,若次短路长度刚好比最短路长1,则答案加上最短路的……

这题简直搞的无语,由于以前只会SPFA模版,并不知道Dij的思路,然而搜到的都是用朴素Dij做的,于是重新看了下离散数学和数据结构的最短路求法——每一次找一个不在S中且离源点最近的点进行拓展,按照这样的顺序得到一系列长度递增(感觉应该是不减的序列)的顺序序列D{i},然后这题怎么套呢。

就分四种情况,比最短路短,跟最短路一样长,比次短路短,跟次短路一样长,第一种情况下要先更新次短再更新最短,然后就是一个比较不复杂的Dij,不过网上的题解都是说循环2*n-1次,但是我觉得是2*n次,因为普通的Dij确实是n-1次因为初始化的时候直接对邻接S起点的点进行了拓展然后让vis[s]=1,但是这题可以不进行拓展只要vis[s]保持0即可,就是让S这个集合初始化为空,起点s的初始化也直接让下面的循环去更新,以前用A*什么的直接爆炸超时……too naive啊,毕竟另外一题范围是10^9,相信数据不会这么友好……

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
const int M=10010;
struct edge
{
int to;
int pre;
int w;
}; edge E[M];
int head[N],tot,n,m;
int d[N][2],way[N][2];
int vis[N][2]; void init()
{
CLR(head,-1);
tot=0;
CLR(d,INF);
CLR(way,0);
CLR(vis,0);
}
inline void add(int s,int t,int w)
{
E[tot].to=t;
E[tot].w=w;
E[tot].pre=head[s];
head[s]=tot++;
}
void Dij(int s)
{
d[s][0]=0;
way[s][0]=1;
for (int i=0; i<2*n; ++i)
{
int cur=-1;
int minm=INF;
int flag=0;
for (int j=1; j<=n; ++j)
{
if(!vis[j][0]&&minm>d[j][0])
{
flag=0;
minm=d[j][0];
cur=j;
}
else if(!vis[j][1]&&minm>d[j][1])
{
flag=1;
minm=d[j][1];
cur=j;
}
}
if(cur==-1)
break;
vis[cur][flag]=1;
for (int j=head[cur]; ~j; j=E[j].pre)
{
int v=E[j].to;
int w=E[j].w;
if(d[v][0]>d[cur][flag]+w)
{
d[v][1]=d[v][0];
way[v][1]=way[v][0]; d[v][0]=d[cur][flag]+w;
way[v][0]=way[cur][flag];
}
else if(d[v][0]==d[cur][flag]+w)
way[v][0]+=way[cur][flag]; else if(d[v][1]>d[cur][flag]+w)
{
d[v][1]=d[cur][flag]+w;
way[v][1]=way[cur][flag];
} else if(d[v][1]==d[cur][flag]+w)
way[v][1]+=way[cur][flag];
}
}
}
int main(void)
{
int tcase,a,b,w,s,t,i;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d%d",&n,&m);
for (i=0; i<m; ++i)
{
scanf("%d%d%d",&a,&b,&w);
add(a,b,w);
}
scanf("%d%d",&s,&t);
Dij(s);
int ans=way[t][0];
if(d[t][0]+1==d[t][1])
ans+=way[t][1];
printf("%d\n",ans);
}
return 0;
}

HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)的更多相关文章

  1. HDU 1688 Sightseeing 【输出最短路+次短路条数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...

  2. HDU 1688 Sightseeing

    题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio. ...

  3. hdu 1688 Sightseeing (最短路径)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. 【最短路】HDU 1688 Sightseeing

    题目大意 给出一个有向图(可能存在重边),求从\(S\)到\(F\)最短路的条数,如果次短路的长度仅比最短路的长度多1,那么再加上次短路的条数. 输入格式 第一行是数据组数\(T\). 对于魅族数据, ...

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

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

  6. hdu 3191 How Many Paths Are There (次短路径数)

    How Many Paths Are There Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  7. hdu 3191 How Many Paths Are There

    http://acm.hdu.edu.cn/showproblem.php?pid=3191 这道题求次短路经和路径数 #include <cstdio> #include <cst ...

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

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

  9. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

随机推荐

  1. ThreadGroup分析

    本文为转载:http://sunboyyyl.blog.163.com/blog/static/2247381201211531712330/ 在Java中每一个线程都归属于某个线程组管理的一员,例如 ...

  2. Java内部类的访问规则

    1.内部类可以直接访问外部类中的成员,包括私有      原因:因为在内部类中持有一个外部类的应用,格式:外部类.this class Outer {     private int x = 1; c ...

  3. Android数据存储之sharedpreferences与Content Provider

    android中对数据操作包含有: file, sqlite3, Preferences, ContectResolver与ContentProvider前三种数据操作方式都只是针对本应用内数据,程序 ...

  4. Linus的redHat在root出现错误:-bash: addgroup: command not found

    在root下无法完成添加用户组操作,如下: [root@host8 ~]# addgroup hadoop-bash: addgroup: command not found 这是的,root下竟然不 ...

  5. JUC回顾之-ScheduledThreadPoolExecutor底层实现原理和应用

    项目中经常使用定时器,比如每隔一段时间清理下线过期的F码,或者应用timer定期查询MQ在数据库的配置,根据不同version实现配置的实时更新等等.但是timer是存在一些缺陷的,因为Timer在执 ...

  6. ytu 1985:C语言实验——保留字母(水题)

    C语言实验——保留字母 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 136  Solved: 59[Submit][Status][Web Board] ...

  7. Java Hour6

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为5 Hour,请各位不吝赐教. Hour6 Jav ...

  8. 12、uwp 开发的零碎总结

    1.在给位 “修正版本号”(Major.Minor.Build.Revision)不能修改. 后, 商店上传失败,描述信息为:Apps are not allowed to have a Versio ...

  9. Eigen相关介绍

    最近在用Matlab处理图像,现在要做的是将其用C++语言进行翻译,由于要进行大量的矩阵计算,就研究了一下可以进行矩阵计算的开源库,详细的介绍可以参照http://my.oschina.net/cvn ...

  10. WPF/Silverlight HierarchicalDataTemplate 模版的使用(转)

    上一篇 对Wpf/Silverlight Template 进行了总结,本篇继续上一篇,主要是介绍 HierarchicalDataTemplate 的使用方法.HierarchicalDataTem ...