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 ...
随机推荐
- angularjs 动态计算平均值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- web超大文件上传
文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...
- [Luogu] 让我们异或吧
https://www.luogu.org/problemnew/show/P2420 异或满足 A ^ B = B ^ A A ^ A = 0 0 ^ A = A #include <cstd ...
- 【组合数学】OI内的排列与组合(简单版)
§1基本原理 △让我们来看下面问题: 从甲地到乙地,可以乘火车,也可以乘汽车,还可以乘轮船.一天中,火车有4班,汽车有2班,轮船有3班.那么,一天中乘坐这些交通工具从甲地到乙地共有多少种不同走法?△分 ...
- windows下去掉快捷方式图标的小箭头的几种方法
去掉快捷方式图标的小箭头的几种方法 第一种: 点开始菜单,点运行,输入以下命令后回车.即可解决 cmd /k reg delete "HKEY_CLASSES_ROOT\lnkfile&qu ...
- 将省市县三级联动的json数据,转化为element-ui能用的格式,并使用
var options=[]; var cities = { '北京': { '北京': ['东城区', '西城区', '崇文区', '宣武区', '朝阳区', '丰台区', '石景山区', '海淀区 ...
- tesseract 安装及使用
安装软件 tesseract下载地址:https://digi.bib.uni-mannheim.de/tesseract/ 安装即可! 安装完成tesseract-ocr后,需要做一下配置 . 在P ...
- Java并发概念-2
一,死锁: 所谓死锁: 是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远在 ...
- Js 中那些 隐式转换
曾经看到过这样一个代码: (!(~+[])+{})[--[~+""][+[]]*[~+[]]+~~!+[]]+({}+[])[[~!+[]*~+[]]] = sb , 你敢相信, ...
- Java并发指南12:深度解读 java 线程池设计思想及源码实现
深度解读 java 线程池设计思想及源码实现 转自 https://javadoop.com/2017/09/05/java-thread-pool/hmsr=toutiao.io&utm_ ...