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. Hudson可扩展持续集成引擎

    参考文章:http://blog.csdn.net/dazhi_100/article/details/11629133 极限编程中一项建议实践便是持续集成,持续集成是指在开发阶段,对项目进行持续性自 ...

  2. LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件

    //file_op.c #include <string.h> #include <stdio.h> #include <stdlib.h> struct info ...

  3. 配置hadoop-1.2.1出现localhost: Error: JAVA_HOME is not set.

    配置hadoop-1.2.1出现localhost: Error: JAVA_HOME is not set. 具体为: hadoop@dy-virtual-machine:~/hadoop-1.2. ...

  4. java基础知识回顾之java Thread类学习(十一)--join方法的理解

    以下面例子说明下面的源码:main 线程 和 A线程,A线程是main线程创建并且启动的,main线程优先级比较高,正在执行:这个时候main线程调用A.join()之后,main线程一直等待,直到A ...

  5. app后端架构设计(转)

    (1)Restful设计原则 Restful风格:RESTfu设计原则,它被Roy Felding提出(在他的”基于网络的软件架构“论文中第五章).而REST的核心原则是将你的API拆分为逻辑上的资源 ...

  6. Jquery和雅虎的YQL服务实现天气预报功能!

    可以直接使用http://www.tianqi.com/plugin/#tjdm定制天气 很多外部网站都有天气预报功能,对于很多企业内部的门户也需要有天气预报功能,但实现天气预报的功能和方式确有所差异 ...

  7. 从C# 到 OC

    字符串 声明: C#: string name = “lwme.cnblogs.com”; Objective-C: NSString *name = @”lwme.cnblogs.com”; 字符串 ...

  8. WebService站点服务的地址

    天气的地址 http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx

  9. java中异步多线程超时导致的服务异常

    在项目中为了提高大并发量时的性能稳定性,经常会使用到线程池来做多线程异步操作,多线程有2种,一种是实现runnable接口,这种没有返回值,一种是实现Callable接口,这种有返回值. 当其中一个线 ...

  10. 2016.6.20 计算机网络复习要点第三章之CSMA/CD协议

    1.最早的以太网是将许多计算机都连接到一根总线上: (1)总线的特点是:当一台计算机发送数据时,总线上的所有计算机都检测到这个数据,这种就是广播通信方式: (2)为了实现在总线上的一对一通信,可以使每 ...