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. E2E test protractor selenium

    E2E Test和传统的Unit Test不同的是:(1)不涉及代码层面,不会去测试某段代码是否正确或者某行代码是否被覆盖(2)它是从用户的角度出发,用来测试一个应用的流程是否符合预期. 一 Sele ...

  2. JAVA日志框架概述

            日志用来记录应用的运行状态以及一些关键业务信息,其重要性不言而喻,通常我们借助于现有的日志框架完成日志输出.目前开源的日志框架很多,常见的有log4j.logback等,有时候我们还会 ...

  3. 「日常训练」Divisibility by Eight(Codeforces Round 306 Div.2 C)

    题意与分析 极简单的数论+思维题. 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #de ...

  4. jmeter的脚本增强之参数化

    jmeter作为一款开源的测试工具,功能广泛,深受测试同胞们的喜爱,这次来讲讲关于如何参数化及其方式.那为什么要进行一个参数化呢,如做压测时,要有大量的数据来模拟用户的真实场景,像登录页面操作,系统是 ...

  5. 初学Direct X(3)

    初学Direct X(3) 1.获取外设输入--键盘以及鼠标 无论是获取鼠标还是键盘的设备,首先得初始化DirectInput,不过先把必要的环境先配置好: 所要用到的头文件以及库文件是(相比于前两次 ...

  6. Python全栈 Web(边框、盒模型、背景)

    原文地址 https://yq.aliyun.com/articles/634926 ......................................................... ...

  7. python 终极篇 --- django 视图系统

    Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误, ...

  8. (python)leetcode刷题笔记05 Longest Palindromic Substring

    5. Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You ...

  9. bug 调试

    系统性能分析中,CPU.内存和 IO 是主要关注项.----系统层面 1. 对于 CPU,如果是常见的 Linux,可以先用 top 命令查看负载状况. top -H  -p [pid] pstree ...

  10. php多进程单例模式下的 MySQL及Redis连接错误修复

    前几天写了个php常驻脚本,主要逻辑如下 //跑完数据后休息60秒 $sleepTime = 60; $maxWorker = 10; while (true) { $htmlModel = new ...