题目链接: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

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.
题目大意:给定一张有向图,求出起点到终点的次短路条数。
思路:很简单的一道题次短路条数模板题, 但是WA了 看讨论说是不能用优先队列dij,但是我不想改了 上个错误模板代码。
代码如下:
 #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

 

最新文章

  1. winSocket数据传输
  2. 乐视云计算基于OpenStack的IaaS实践
  3. CSS3动画(动画已丢,看原文)
  4. JavaWeb笔记——利用过滤器实现页面静态化
  5. 【HDOJ】1239 Calling Extraterrestrial Intelligence Again
  6. Java经典23创意模式设计模式(两)
  7. Jumpserver之快速入门
  8. CodeForces755F 贪心 + 多重背包二进制优化
  9. JAVA核心技术I---JAVA基础知识(函数)
  10. Kafka概述及安装部署
  11. .net framework 项目 build 出现 未能加载文件或程序集“netfx.force.conflicts”或它的某一个依赖项
  12. 20135323符运锦----LINUX第三次实践:程序破解
  13. NatApp开启HTTPS访问方式
  14. shell脚本死循环检查是否有特定的路由,若存在进行删除操作
  15. 【做题】CFedu41G. Partitions——推式子
  16. kali删除软件
  17. 使用 swoole 加速你的 laravel
  18. 【WPF/C#】拖拽Image图片控件
  19. DLL入口函数
  20. Python并行编程(六):线程同步之条件

热门文章

  1. ES6-12.Symbol
  2. Oracle 物理结构(二) 文件-口令文件
  3. 02_通过位置变量创建 Linux 系统账户及密码
  4. 浏览器console中加入jquery,测试选择元素
  5. os/exec
  6. [Luogu] 家族
  7. scrapy框架之下载中间件
  8. DQL:查询表中数据
  9. Windows下MongoDB的安装过程及基本配置
  10. 问题MySQL Error (2013): Lost connection to MySQL server at waiting for initial communication packet