HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)
Sightseeing
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1023 Accepted Submission(s): 444
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.
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.
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变形求次短路条数)的更多相关文章
- HDU 1688 Sightseeing 【输出最短路+次短路条数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...
- HDU 1688 Sightseeing
题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio. ...
- hdu 1688 Sightseeing (最短路径)
Sightseeing Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 【最短路】HDU 1688 Sightseeing
题目大意 给出一个有向图(可能存在重边),求从\(S\)到\(F\)最短路的条数,如果次短路的长度仅比最短路的长度多1,那么再加上次短路的条数. 输入格式 第一行是数据组数\(T\). 对于魅族数据, ...
- poj 3463 Sightseeing(次短路+条数统计)
/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...
- 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 ...
- hdu 3191 How Many Paths Are There
http://acm.hdu.edu.cn/showproblem.php?pid=3191 这道题求次短路经和路径数 #include <cstdio> #include <cst ...
- poj 3463/hdu 1688 求次短路和最短路个数
http://poj.org/problem?id=3463 http://acm.hdu.edu.cn/showproblem.php?pid=1688 求出最短路的条数比最短路大1的次短路的条数和 ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
随机推荐
- HDU 5512 Pagodas (gcd)
题目:传送门. 题意:t组数据,每组数据给定n,a,b,a!=b,在[1,n]的这些点中,每次选取a+b或a-b或b-a点,选取过的点在下次选取的时候可以当做ab来用继续选取,谁不能继续选取谁就输,问 ...
- MFC中挂起线程和恢复线程
DWORD SuspendThread ( HANDLE hThread ); //挂起线程DWORD ResumeThread ( HANDLE hThread ); //恢复线程 比如说我 ...
- dfs常见的配置文件中的value与description
照抄于网络: name value description dfs.namenode.logging.level info The logging level for dfs namenode. Ot ...
- 何时使用hadoop fs、hadoop dfs与hdfs dfs命令(转)
hadoop fs:使用面最广,可以操作任何文件系统. hadoop dfs与hdfs dfs:只能操作HDFS文件系统相关(包括与Local FS间的操作),前者已经Deprecated,一般使用后 ...
- linux编译中的常见问题
转linux编译中的常见问题 错误提示:Makefile:2: *** 遗漏分隔符 . 停止. 原因makefile中 gcc语句前 缺少一个 tab分割符 错误提示: bash: ./makefil ...
- jquery.validate.unobtrusive.js插件作用
在 ASP.NET MVC 中启用 Unobtrusive JavaScript 功能,可以在运行时由服务器端根据Model中设置的验证规则,自动生成客户端验证js代码(结合jquery.valida ...
- WPF控件委托
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)delegate { //要执行的代码 });
- 电赛初探(二)——语音采集回放系统
一.系统结构 1.基本要求 (1)话音/功率放大器增益均可调: (2)带通滤波器:通带为300Hz-3.4kHz : (3)ADC:采样频率f s=8kHz,字长不小于8位: (4)语音存储时间≥10 ...
- SQL SERVER 与ACCESS、EXCEL的数据转换
--Excel导入到SQL的一个新思路: /*比如Excel有两列,A列和B列需要导入到SQL表中,反正我已经有几年不用DTS之类的工具了. 在Excel中的新的一列中,直接写公式 =CONCATEN ...
- Python开发的10个小贴士
下面是十个Python中很有用的贴士和技巧.其中一些是初学这门语言常常会犯的错误. 注意:假设我们都用的是Python 3 1. 列表推导式 你有一个list:bag = [1, 2, 3, 4, 5 ...