HDU3191 【输出次短路条数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3191
How Many Paths Are There
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2128 Accepted Submission(s):
749
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.
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.
those second shortest paths in one line. Separate them with a single
space.
#include<stdio.h>
#include<string.h>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
const int inf = 0x3f3f3f3f;
using namespace std; int n, m, st, ed;
int head[MAXN], cnt;
int dis[][MAXN], num[][MAXN], vis[][MAXN]; struct Edge
{
int to, next, w;
}edge[MAXM]; struct Node
{
int id, dis, p;
bool operator < (const Node &a)const
{
return dis > a.dis;
}
}no; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt;
} void dij()
{
mem(vis, );
priority_queue<Node> Q;
for(int i = ; i < n; i ++)
{
dis[][i] = dis[][i] = inf;
num[][i] = num[][i] = ;
}
dis[][st] = ;
num[][st] = ;
no.p = , no.id = st, no.dis = ;
Q.push(no);
while(!Q.empty())
{
Node a = Q.top();
Q.pop();
if(vis[a.p][a.id])
continue;
vis[a.p][a.id] = ;
for(int i = head[a.id]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dis[][to] > dis[a.p][a.id] + edge[i].w)
{
dis[][to] = dis[][to];
dis[][to] = dis[a.p][a.id] + edge[i].w;
num[][to] = num[][to];
num[][to] = num[a.p][a.id];
no.p = , no.dis = dis[][to], no.id = to;
Q.push(no);
no.p = , no.dis = dis[][to], no.id = to;
Q.push(no);
}
else if(dis[][to] == dis[a.p][a.id] + edge[i].w)
num[][to] += num[a.p][a.id];
else if(dis[][to] > dis[a.p][a.id] + edge[i].w)
{
dis[][to] = dis[a.p][a.id] + edge[i].w;
num[][to] = num[a.p][a.id];
no.p = , no.dis = dis[][to], no.id = to;
Q.push(no);
}
else if(dis[][to] == dis[a.p][a.id] + edge[i].w)
num[][to] += num[a.p][a.id];
}
}
} int main()
{
while(scanf("%d%d%d%d", &n, &m, &st, &ed) != EOF)
{
mem(head, -), cnt = ;
for(int i = ; i <= m; i ++)
{
int a, b, c;//有向图
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
dij();
printf("%d %d\n", dis[][ed], num[][ed]);
}
return ;
}
HDU3191
HDU3191 【输出次短路条数】的更多相关文章
- HDU 1688 Sightseeing 【输出最短路+次短路条数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...
- 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 ...
- poj 3463 Sightseeing(次短路+条数统计)
/* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...
- Spark Mllib里如何程序输出数据集的条数(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第17章 决策树多元分类UCI Covertype数据集
- Linux Find Out Last System Reboot Time and Date Command 登录安全 开关机 记录 帐号审计 历史记录命令条数
Linux Find Out Last System Reboot Time and Date Command - nixCraft https://www.cyberciti.biz/tips/li ...
- HDU1688-POJ3463-Sightseeing(求次短路的条数)
题意 求出最短路和次短路的条数,当次短路比最短路长度小1时,输出条数之和,反之输出最短路条数. 题解 dis1[],cnt1[],dis2[],cnt2[] 分别表示最短路的长度和条数,次短路的长度 ...
- HDU 3416 Marriage Match IV (求最短路的条数,最大流)
Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...
- Hibernate自定义数据库查询(排序、输出条数)
Hibernate数据库操作类(eg:TexDAO.java) /* * queryString HQL语句,first开始条数, max输出条数 ,norder排序 * 例: List lis = ...
- 最短路和次短路的条数(dijstra算法或spfa算法)POJ3463
http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
随机推荐
- mybatis 批量删除添加
mybatis使用foreach进行批量插入和删除操作 转发与 https://www.cnblogs.com/Amaris-Lin/p/8615977.html 一.批量插入 1. ...
- BZOJ 2169 连边 DP
思路:DP 提交:\(1\)次(课上刚讲过) 题解: 如果不管重边的话,我们设\(f[i][j]\)表示连了\(i\)条边,\(j\)个点的度数是奇数的方案数,那么显然我们可以分三种状态转移: \(f ...
- jQuery.proxy(function,context)
jQuery.proxy(function,context) 概述 jQuery 1.4 新增.返回一个新函数,并且这个函数始终保持了特定的作用域.大理石平台检定规程 当有事件处理函数要附加到元素上, ...
- 024_STM32程序移植之_ESP8266_TCP
(一)实验目的:编写电脑软件通过ESP8266传输数据给STM32的,下面这张图 (二)上面只是简单地图,视频比较全面 视频教程:https://v.qq.com/x/page/o0829zs7iop ...
- 富文本编辑器粘贴word内容
很多时候我们用一些管理系统的时候,发布新闻.公告等文字类信息时,希望能很快的将word里面的内容直接粘贴到富文本编辑器里面,然后发布出来.减少排版复杂的工作量. 下面是借用百度doc 来快速实现这个w ...
- Gradle 发布 Jar 到 Archiva 时提示不能 Overwriting released artifacts is not allowed
系统提示错误信息: Received status code 409 from server: Overwriting released artifacts is not allowed. 这是在 A ...
- 【概率论】3-2:连续分布(Continuous Distributions)
title: [概率论]3-2:连续分布(Continuous Distributions) categories: Mathematic Probability keywords: Continuo ...
- 解决“cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:356:...”
主要是图片路径中“文件夹分隔符”使用的错误 将“\”改成“/”就好了 修改后的测试代码如下:x.py #导入cv模块 import cv2 as cv #读取图像,支持 bmp.jpg.png.tif ...
- JS的十大排序算法
名词解释: n: 数据规模k:“桶”的个数In-place: 占用常数内存,不占用额外内存Out-place: 占用额外内存稳定性:排序后2个相等键值的顺序和排序之前它们的顺序相同 冒泡排序(Bub ...
- 安装APK时报错:Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
安装APK时报错:Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI] 可以使用adb install -t 解决 对于已经在手机的文件可以使用pm ...