博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu6181 How Many Paths Are There(次短路条数[模板])
阅读量:5154 次
发布时间:2019-06-13

本文共 3346 字,大约阅读时间需要 11 分钟。

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

分析:

次短路条数板子
简单总结一下:
次短路条数选择dij,
每个节点记录两个状态:最短路和次短路,同时分别记录条数
dij进行2*n-1次
每次找出所有可用路径中的最短路径
在这个基础上进行更改
设当前的最短距离是x=dis[k][flag]+way[i].v
一共有四种情况:
x < dis[j][0]
x==dis[j][0]
x < dis[j][1]
x==dis[j][1]
分别转移就好了

tip

因为结点的编号从0开始,我的处理方法是把ta变成从1开始

不要忘了if (mn==INF) break;
不存在最短的转移点,就退出for循环,
不要忘了bool数组的置否

注意这句话

You would be given a directed graph G

译:给出一个有向图

又是有向边

可见,英文题面中许多条件不会再hint中中专门给出,一定要

学好英语,仔细读题

听学长说北大冬令营题面几乎都是英文的,所以好好学英文吧

这里写代码片#include
#include
#include
using namespace std;const int INF=0x3333333;const int N=100;struct node{ int x,y,v,nxt;};node way[N*N];int cnt[N][2],st[N],tot,dis[N][2],n,m,s,t;bool p[N][2];void add(int u,int w,int z){ tot++; way[tot].x=u;way[tot].y=w;way[tot].v=z;way[tot].nxt=st[u];st[u]=tot;}void dij(int s,int t){ int i,j,k,flag; memset(dis,0x33,sizeof(dis)); memset(p,1,sizeof(p)); memset(cnt,0,sizeof(cnt)); dis[s][0]=0; cnt[s][0]=1; for (i=1;i<=2*n-1;i++) { int mn=INF; for (j=1;j<=n;j++) if (p[j][0]&&dis[j][0]
mn+w) { dis[y][1]=mn+w; cnt[y][1]=cnt[k][flag]; } else if (dis[y][1]==mn+w) { cnt[y][1]+=cnt[k][flag]; } } } printf("%d %d\n",dis[t][1],cnt[t][1]);}int main(){ while (scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF) { s++; t++; tot=0; memset(st,0,sizeof(st)); for (int i=1;i<=m;i++) { int u,w,z; scanf("%d%d%d",&u,&w,&z); u++; w++; add(u,w,z); //add(w,u,z); } dij(s,t); } return 0;}

转载于:https://www.cnblogs.com/wutongtong3117/p/7673226.html

你可能感兴趣的文章
学习笔记-任意用户密码重置10钟常见姿势
查看>>
msf假冒令牌
查看>>
留着看
查看>>
Centos7 搭建sonarQube
查看>>
mysql中计算两个日期的时间差函数TIMESTAMPDIFF用法
查看>>
JAVA中通过Hibernate-Validation进行参数验证
查看>>
使用lagg做链路聚合与故障转移
查看>>
VS JavaScript 提示设置
查看>>
在vue项目npm run build后,index.html中引入css和js 报MIME type问题
查看>>
python的数据结构
查看>>
HTML5已成为主流的移动互联网云计算编程格式
查看>>
(转)从客户端中检测到有潜在危险的 Request.Form 值
查看>>
How to fix updating ubuntu apt-get problem
查看>>
以软件开发生命周期的过程来说明不同测试的使用情况
查看>>
Log Structured Merge Trees(LSM) 原理
查看>>
mysql中的事务
查看>>
Linux内核配置Kconfig语法
查看>>
NSQ:分布式的实时消息平台
查看>>
linux 开机启动nginx
查看>>
Java程序如何自动在线升级
查看>>