How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1010    Accepted Submission(s): 332

Problem Description
  oooccc1 is a Software Engineer who has to ride to the work place every Monday through Friday. For a long period, he went to office with the shortest path because he loves to sleep late…Time goes by, he find that he should have some changes as you could see, always riding with the same path is boring.
  One day, oooccc1 got an idea! Why could I take another path? Tired at all the tasks he got, he got no time to carry it out. As a best friend of his, you’re going to help him!
  Since oooccc1 is now getting up earlier, he is glad to take those paths, which are a little longer than the shortest one. To be precisely, you are going to find all the second shortest paths.
  You would be given a directed graph G, together with the start point S which stands for oooccc’1 his house and target point E presents his office. And there is no cycle in the graph. Your task is to tell him how long are these paths and how many there are.
 
Input
There are some cases. Proceed till the end of file.
The first line of each case is three integers N, M, S, E (3 <= N <= 50, 0 <= S , E <N)
N stands for the nodes in that graph, M stands for the number of edges, S stands for the start point, and E stands for the end point.
Then M lines follows to describe the edges: x y w. x stands for the start point, and y stands for another point, w stands for the length between x and y. 
All the nodes are marked from 0 to N-1.
 
Output
For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.
 
Sample Input
3 3 0 2
0 2 5
0 1 4
1 2 2
 
Sample Output
6 1
 
Author
ZSTU
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2363 2377 2433 2833 1688 
 

题意:

给出一个有向图,求其次短路径数。

最短路径:

先求次短路径:

从源点到汇点构图求出所有ds[i];

从汇点到源点构图求出所有de[i]; ds[i]、de[i]分别为源点到其他点最短路长,汇点到其他点最短路长

然后枚举所有边,当枚举到的边(i,j)长度ds[i]+de[j]+(i,j)仅仅大于源点到汇点的最短路长时其为次短路径

求出次短路长后dfs求其数量,犯了点错误TLE了好多次.不过dfs的耗时有点大

代码:

 //453MS    256K    2508 B    C++
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x7ffffff
#define N 55
using namespace std;
struct node{
int v,d;
node(int a,int b){
v=a;d=b;
}
};
vector<node>Vs[N],Ve[N],V[N];
int vis[N],dd;
int n,cnt,s,e;
void spfa_s(int u,int d[])
{
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
d[i]=inf;
d[u]=;
vis[u]=;
queue<int>Q;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
vis[u]=;
int m=Vs[u].size();
for(int i=;i<m;i++){
int v=Vs[u][i].v;
int w=Vs[u][i].d;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
void spfa_e(int u,int d[])
{
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
d[i]=inf;
d[u]=;
vis[u]=;
queue<int>Q;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
vis[u]=;
int m=Ve[u].size();
for(int i=;i<m;i++){
int v=Ve[u][i].v;
int w=Ve[u][i].d;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
void dfs(int u,int w)
{
if(u==e && w==dd) cnt++;
if(u==e || w>=dd) return;
int m=Vs[u].size();
for(int i=;i<m;i++){
int v=Vs[u][i].v;
int tw=Vs[u][i].d;
if(!vis[v]){
vis[v]=;
dfs(v,tw+w);
vis[v]=;
}
}
}
int main(void)
{
int ds[N],de[N];
int m;
int u,v,w;
while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
for(int i=;i<n;i++){
Vs[i].clear();
Ve[i].clear();
}
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
Vs[u].push_back(node(v,w));
Ve[v].push_back(node(u,w));
}
spfa_s(s,ds);
spfa_e(e,de);
dd=inf;
for(int i=;i<n;i++)
for(int j=;j<Vs[i].size();j++){
int td=ds[i]+de[Vs[i][j].v]+Vs[i][j].d;
if(td!=ds[e] && td<dd) dd=td;
}
cnt=;
memset(vis,,sizeof(vis));
dfs(s,);
printf("%d %d\n",dd,cnt);
}
return ;
}

==========================================================================================================================================================================================================

发现有次短路模板,时间复杂度比我的好多了.!

 //15MS    256K    2291 B    C++
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x7ffffff
#define N 55
using namespace std;
struct edge{
int v,w;
edge(int a,int b){
v=a;w=b;
}
};
struct node{
int v,w;
int mark;
bool operator < (const node &p) const{
if(p.w!=w) return p.w<w;
return p.v<v;
}
};
vector<edge>V[N];
int n,m,s,e;
int d[N][]; //记录最短路和次短路距离
int dp[N][]; //记录最短路和次短路条数
int vis[N][];
void dijkstra(int s) //优先队列实现dij
{
for(int i=;i<n;i++)
d[i][]=d[i][]=inf;
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
priority_queue<node>Q;
node p,q;
d[s][]=;
dp[s][]=;
p.w=,p.mark=,p.v=s;
Q.push(p);
while(!Q.empty()){
p=Q.top();
Q.pop();
if(vis[p.v][p.mark]) continue;
vis[p.v][p.mark]=;
for(int i=;i<V[p.v].size();i++){
int v=V[p.v][i].v;
int w=V[p.v][i].w;
if(!vis[v][] && d[v][]>p.w+w){ //更新最短路长
if(d[v][]!=inf){ //原最短路可为次短路
d[v][]=d[v][];
dp[v][]=dp[v][];
q.v=v,q.w=d[v][],q.mark=;
Q.push(q);
}
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.v=v,q.w=d[v][],q.mark=;
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){ //更新最短路长数
dp[v][]+=dp[p.v][p.mark];
}else if(!vis[v][] && d[v][]>p.w+w){ //更新次短路长
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.w=d[v][],q.v=v,q.mark=;
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){ //更新次短路长数
dp[v][]+=dp[p.v][p.mark];
}
}
}
}
int main(void)
{
int u,v,w;
while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
for(int i=;i<n;i++)
V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
V[u].push_back(edge(v,w));
}
dijkstra(s);
printf("%d %d\n",d[e][],dp[e][]);
}
return ;
}

hdu 3191 How Many Paths Are There (次短路径数)的更多相关文章

  1. hdu 3191 How Many Paths Are There

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

  2. 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) Tota ...

  3. 【LeetCode每天一题】Unique Paths(唯一的路径数)

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The ...

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

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

  5. hdu 3191 次短路的长度和个数

    http://acm.hdu.edu.cn/showproblem.php?pid=3191 求次短路的长度和个数 相关分析在这里http://blog.csdn.net/u012774187/art ...

  6. HDU 6181:Two Paths(次短路)

    Two Paths Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others) Total S ...

  7. 【hdu 6181】Two Paths

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6181 [题意] 让你求从1到n的次短路 [题解] 模板题; 因为点可以重复走; 则一定会有次短路. di ...

  8. HDU 3191 次短路长度和条数

    http://www.cnblogs.com/wally/archive/2013/04/16/3024490.html http://blog.csdn.net/me4546/article/det ...

  9. HDU 6181:Two Paths(A* + SPFA)

    题目链接 题意 给出n个点m条边的无向图,求次短路. 思路 和 POJ 2449 类似,只不过大小要开成long long. #include <bits/stdc++.h> using ...

随机推荐

  1. 成都Uber优步司机奖励政策(2月17日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. nodejs 实现套接字服务

    nodejs实现套接字服务     一 什么是套接字 1.套接字允许一个进程他通过一个IP地址和端口与另一个进程通信,当你实现对运行在同一台服务器上的两个不同进程的进程间通信或访问一个完全不同的服务器 ...

  3. NSNull Crash处理 (NullSafe 的原理)

    问题场景 后端返回的数据中总会出现一些NSNull类型,当我们一处理程序就会崩溃,因此想到把返回的数据中的NSNull类型全部转换成@""空字符串 (1)原始的json串:后端返回 ...

  4. dva 路由跳转

    1.从props取出并传递history 取 const { history } = this.props 用 <button onClick={ () => history.push(' ...

  5. windows环境下jmeter生成测试报告

    1.要求 jmeter需要在3.0版本以上 jdk1.7以上 需要准备脚本文件,即jmx文件 2.进入cmd界面 3.进入jmeter的bin目录 cd:\xxxx\apache-jmeter-4.0 ...

  6. Selenium 入门到精通系列:二

    Selenium 入门到精通系列 PS:用户登录 例子 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2019-04-23 16:12 ...

  7. 原生js实现轮播图原理

    轮播图的原理1.图片移动实现原理:利用浮动将所有所有照片依次排成一行,给这一长串图片添加一个父级的遮罩,每次只显示一张图,其余的都隐藏起来.对图片添加绝对定位,通过控制left属性,实现照片的移动. ...

  8. 记一次Log4j2日志无法输出的 心酸史

    问题描述:部分日志无法输出到日志文件中. 项目中的代码: @Resource private ConfigInfo configInfo; private static final Logger lo ...

  9. spark集群安装部署

    通过Ambari(HDP)或者Cloudera Management (CDH)等集群管理服务安装和部署在此不多介绍,只需要在界面直接操作和配置即可,本文主要通过原生安装,熟悉安装配置流程. 1.选取 ...

  10. commons-lang源码解析之StringUtils

    apache的commons工具包是平时使用最多的工具包之一,对其实现方式需要具体了解.commons-lang version 3.1 empty和blank的区别 StringUtils中判断St ...