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. letsencrypt证书-使用certbot申请wildcard证书

    目录 1. certbot安装 2. 认证方式 3. 运行命令 4. 更新 1:下载 2:配置 3:申请证书 4:续期证书 1. certbot安装 cd /usr/local/src wget ht ...

  2. protected修饰符详解

    protected这个修饰符,各大参考书都会这样说:访问权限为类内,包内和子类,因此在父类中定义的方法和成员变量如果为protected修饰的,是可以在不同包中的子类进行访问的,示例代码如下: pac ...

  3. android学习七 菜单

    1.菜单分类 常规菜单 子菜单 上下文菜单 图标菜单 辅助菜单 交替菜单 2.菜单类 andriod.view.menu   3.菜单的参数     名称:字符串标题     菜单ID:整数     ...

  4. “腾讯WeTest助力《龙珠直播》盘点APP质量问题”

    WeTest 导读 据调查数据表明,移动端用户在使用APP时如果遇到了闪退等兼容性问题,20%的用户会选择直接卸载. 2016年,被称为中国直播元年.随着各类直播平台的疯狂生长与扩散,直播产品在内容, ...

  5. 使用 Fiddler工具模拟post四种请求数据

    post请求主体详解: 对于get请求来说没有请求主体entity-body.对于post请求而言,不会对发送请求的数据格式进行限制,理论上你可以发任意数据,但是服务器能不能处理就是另一回事了.服务器 ...

  6. Java开发工程师(Web方向) - 03.数据库开发 - 第2章.数据库连接池

    第2章--数据库连接池 数据库连接池 一般而言,在实际开发中,往往不是直接使用JDBC访问后端数据库,而是使用数据库连接池的机制去管理数据库连接,来实现对后端数据库的访问. 建立Java应用程序到后端 ...

  7. OpenMPI 集群配置

    现在有2台机器,希望可以尝试一下在多台机器上跑MPI的感觉,所以跑之前就得配置,先参考网址: https://www.cnblogs.com/awy-blog/p/3402949.html: 1. 配 ...

  8. IntelliJ IDEA 2018 for MAC安装及破解

    ---------------------说在前面-------------------------- IntelliJ IDEA 2018 版本为2018.1.4 教程按照下载安装sdk.破解两部分 ...

  9. JAVA集合类(大公司面试喜欢问的)

     分类: 核心JAVA(11)  版权声明:本文为博主原创文章,未经博主允许不得转载. 看了一些所谓大公司的Java面试问题,发现对于JAVA集合类的使用都比较看重似的,而自己在这方面还真的是所真甚少 ...

  10. 压力测试工具-webbench

    简述 偶然情况下看到一款性能测试工具webbench,看着挺不错的记录一下安装过程,在以后项目上线过程中可以压一压一些页面的并发情况,对项目性能有个大致的了解. 原理 webbench首先fork出多 ...