HDU3191-How many paths are there(次短路的长度及其个数)
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 2Sample Output
6 1题解:题目已经说明题意了;我们可以记录最短路和短路的长度及其次数并不断更新他们。每次更新是无非5种情况:比最小值小,等于最小值,大于最小值小于次小值,等于次小值,大于次小值;
AC代码为:
/*
题意:给你N个点M条有向边,开始点s,终点e,求 s到e的次最短路,输出次最短路的长度和条数
*/
#include<bits/stdc++.h>
using namespace std;
#define MAX 1100
#define INF 999999999
struct edge
{
    int from,to,w;
};
vector<edge> edges;
vector<int> G[MAX];
int m,n;
int dis[MAX][2],vis[MAX][2],cnt[MAX][2];
int s,e;
void addedge(int x,int y,int w)
{
    edge a={x,y,w};
    edges.push_back(a);
    G[x].push_back(edges.size()-1);
 
}
void dijkstra(int x,int y)
{
    for(int i=0;i<=n;i++)
    {
        dis[i][0]=dis[i][1]=INF;
        cnt[i][0]=cnt[i][1]=INF;
    }
    dis[x][0]=0;
    cnt[x][0]=1;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n*2;i++)
    {
        int min=INF,u=-1,flag;
        for(int j=0;j<n;j++)
        {
            if(!vis[j][0]&&min>dis[j][0])
            {
                min=dis[j][0];
                flag=0;
                u=j;
            }
            else if(!vis[j][1]&&min>dis[j][1])
            {
                min=dis[j][1];
                flag=1;
                u=j;
            }
        }
        if(u==-1) break;
        vis[u][flag]=1;
        
        for(int j=0;j<G[u].size();j++)
        {
            edge v=edges[G[u][j]];
            if(min+v.w<dis[v.to][0])
            {
                dis[v.to][1]=dis[v.to][0];
                cnt[v.to][1]=cnt[v.to][0];
                dis[v.to][0]=min+v.w;
                cnt[v.to][0]=cnt[u][flag];
            }
            else if(min+v.w==dis[v.to][0]) cnt[v.to][0]+=cnt[u][flag];
            else if(min+v.w==dis[v.to][1]) cnt[v.to][1]+=cnt[u][flag];
            else if(min+v.w<dis[v.to][1])
            {
                dis[v.to][1]=min+v.w;
                cnt[v.to][1]=cnt[u][flag];
            }
        }
        
    }
    printf("%d %d\n",dis[e][1],cnt[e][1]);
}
int main()
{
   while(scanf("%d %d %d %d",&n,&m,&s,&e)!=EOF)
   {    
        for(int i=0;i<=n;i++) G[i].clear();
        edges.clear();
        for(int i=1;i<=m;i++)
        {
            int x,y,w;
            scanf("%d %d %d",&x,&y,&w);
            addedge(x,y,w);
        }
        dijkstra(s,e);
   }
   return 0;
}
HDU3191-How many paths are there(次短路的长度及其个数)的更多相关文章
- hdu 3191 次短路的长度和个数
		http://acm.hdu.edu.cn/showproblem.php?pid=3191 求次短路的长度和个数 相关分析在这里http://blog.csdn.net/u012774187/art ... 
- COJ 0579 4020求次短路的长度
		4020求次短路的长度 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 在一个地图上共有N个路口(编号分别为1到N),R条道路( ... 
- Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
		题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ... 
- Codeforces 545E. Paths and Trees 最短路
		E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ... 
- Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路
		E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ... 
- Codeforces 545E. Paths and Trees[最短路+贪心]
		[题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ... 
- POJ 3463 有向图求次短路的长度及其方法数
		题目大意: 希望求出走出最短路的方法总数,如果次短路只比最短路小1,那也是可取的 输出总的方法数 这里n个点,每个点有最短和次短两种长度 这里采取的是dijkstra的思想,相当于我们可以不断找到更新 ... 
- HDU 6181:Two Paths(次短路)
		Two Paths Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others) Total S ... 
- hdu3191+hdu1688(求最短路和次短路条数,模板)
		hdu3191题意:求出次短路的长度和条数 #include<iostream> #include<cstdio> #include<cstring> #inclu ... 
随机推荐
- IDEA中WEB项目本地调试和发布的配置分开配置
			一个Web项目,开发的时候设置了一些本地内容,比如IP地址,还有本地目录等.开发完成后,要发布到服务器上时,这些本地相关的配置,就需要配置成服务器上IP或目录. 原先的做法就是部署打包的时候,把相关的 ... 
- 为企业应用开发提速,写给企业IT部门的低代码开发基础知识
			简介:应用程序开发长期以来一直是IT部门和业务部门面临的问题. IT部门总是被新的应用程序需求弄得不堪重负.他们不可能完成业务部门想要完成的每一个项目. 同时,业务部门的用户厌倦了等待,并开始完全绕过 ... 
- Redis 工具 redis-port 使用
			redis-port 是一个 Redis 工具,通过解析 rdb 文件,实现 Redis 主节点和从节点的数据同步. 摘要: 一个可以将redis主从集群,cluster上的数据实时迁移到 cod ... 
- VLAN实验(2)Trunk接口
			1.选择1台S5700.2台S3700和4台pc机,并根据实验编址完成此拓扑图. 2.启动设备,检查设备的连通性: 由于现在我们还没有划分VLAN,这5台PC,还在同一个VLAN中,现在我们启动所有的 ... 
- linux文件时间
			Linux 查看文件修改时间(精确到秒)(简单) ls --full-time 查看文件时间戳命令:stat test.txt linux 下查看文件修改时间 等(详细) 查看文件时间戳命令:stat ... 
- InfluxDB 聚合函数实用案例
			InfluxDB 聚合函数实用案例 文章大纲 InfluxDB 简介 InfluxDB是GO语言编写的分布式时间序列化数据库,非常适合对数据(跟随时间变化而变化的数据)的跟踪.监控和分析.在我们的项目 ... 
- 如何使用Sping Data JPA更新局部字段
			问题描述 在更新数据时,有时候我们只需要更新一部分字段,其他字段保持不变.Spring Data JPA并未提供现成的接口,直接使用save()更新会导致其他字段被Null覆盖掉. 解决办法 通常有两 ... 
- Alibaba Nacos 学习(五):K8S Nacos搭建,使用nfs
			Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ... 
- PL真有意思(五):数据类型
			前言 现在大多数程序设计语言中都有表达式和/或对象的类型概念.类型起着两种主要作用: 为许多操作提供了隐含的上下文信息,使程序员可以在许多情况下不必显示的描述这种上下文.比如int类型的两个对象相加就 ... 
- HTTPS 原理分析——带着疑问层层深入
			HTTPS 随着 HTTPS 建站的成本下降,现在大部分的网站都已经开始用上 HTTPS 协议.大家都知道 HTTPS 比 HTTP 安全,也听说过与 HTTPS 协议相关的概念有 SSL .非对称加 ... 
