hdu 3191 How Many Paths Are There (次短路径数)
How Many Paths Are There
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1010 Accepted Submission(s): 332
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.
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.
题意:
给出一个有向图,求其次短路径数。
最短路径:
先求次短路径:
从源点到汇点构图求出所有ds[i];
从汇点到源点构图求出所有de[i]; ds[i]、de[i]分别为源点到其他点最短路长,汇点到其他点最短路长
然后枚举所有边,当枚举到的边(i,j)长度ds[i]+de[j]+(i,j)仅仅大于源点到汇点的最短路长时其为次短路径
求出次短路长后dfs求其数量,犯了点错误TLE了好多次.不过dfs的耗时有点大
代码:
//453MS 256K 2508 B C++
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x7ffffff
#define N 55
using namespace std;
struct node{
int v,d;
node(int a,int b){
v=a;d=b;
}
};
vector<node>Vs[N],Ve[N],V[N];
int vis[N],dd;
int n,cnt,s,e;
void spfa_s(int u,int d[])
{
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
d[i]=inf;
d[u]=;
vis[u]=;
queue<int>Q;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
vis[u]=;
int m=Vs[u].size();
for(int i=;i<m;i++){
int v=Vs[u][i].v;
int w=Vs[u][i].d;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
void spfa_e(int u,int d[])
{
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
d[i]=inf;
d[u]=;
vis[u]=;
queue<int>Q;
Q.push(u);
while(!Q.empty()){
u=Q.front();
Q.pop();
vis[u]=;
int m=Ve[u].size();
for(int i=;i<m;i++){
int v=Ve[u][i].v;
int w=Ve[u][i].d;
if(d[v]>d[u]+w){
d[v]=d[u]+w;
if(!vis[v]){
vis[v]=;
Q.push(v);
}
}
}
}
}
void dfs(int u,int w)
{
if(u==e && w==dd) cnt++;
if(u==e || w>=dd) return;
int m=Vs[u].size();
for(int i=;i<m;i++){
int v=Vs[u][i].v;
int tw=Vs[u][i].d;
if(!vis[v]){
vis[v]=;
dfs(v,tw+w);
vis[v]=;
}
}
}
int main(void)
{
int ds[N],de[N];
int m;
int u,v,w;
while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
for(int i=;i<n;i++){
Vs[i].clear();
Ve[i].clear();
}
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
Vs[u].push_back(node(v,w));
Ve[v].push_back(node(u,w));
}
spfa_s(s,ds);
spfa_e(e,de);
dd=inf;
for(int i=;i<n;i++)
for(int j=;j<Vs[i].size();j++){
int td=ds[i]+de[Vs[i][j].v]+Vs[i][j].d;
if(td!=ds[e] && td<dd) dd=td;
}
cnt=;
memset(vis,,sizeof(vis));
dfs(s,);
printf("%d %d\n",dd,cnt);
}
return ;
}
==========================================================================================================================================================================================================
发现有次短路模板,时间复杂度比我的好多了.!
//15MS 256K 2291 B C++
#include<iostream>
#include<queue>
#include<vector>
#define inf 0x7ffffff
#define N 55
using namespace std;
struct edge{
int v,w;
edge(int a,int b){
v=a;w=b;
}
};
struct node{
int v,w;
int mark;
bool operator < (const node &p) const{
if(p.w!=w) return p.w<w;
return p.v<v;
}
};
vector<edge>V[N];
int n,m,s,e;
int d[N][]; //记录最短路和次短路距离
int dp[N][]; //记录最短路和次短路条数
int vis[N][];
void dijkstra(int s) //优先队列实现dij
{
for(int i=;i<n;i++)
d[i][]=d[i][]=inf;
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
priority_queue<node>Q;
node p,q;
d[s][]=;
dp[s][]=;
p.w=,p.mark=,p.v=s;
Q.push(p);
while(!Q.empty()){
p=Q.top();
Q.pop();
if(vis[p.v][p.mark]) continue;
vis[p.v][p.mark]=;
for(int i=;i<V[p.v].size();i++){
int v=V[p.v][i].v;
int w=V[p.v][i].w;
if(!vis[v][] && d[v][]>p.w+w){ //更新最短路长
if(d[v][]!=inf){ //原最短路可为次短路
d[v][]=d[v][];
dp[v][]=dp[v][];
q.v=v,q.w=d[v][],q.mark=;
Q.push(q);
}
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.v=v,q.w=d[v][],q.mark=;
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){ //更新最短路长数
dp[v][]+=dp[p.v][p.mark];
}else if(!vis[v][] && d[v][]>p.w+w){ //更新次短路长
d[v][]=p.w+w;
dp[v][]=dp[p.v][p.mark];
q.w=d[v][],q.v=v,q.mark=;
Q.push(q);
}else if(!vis[v][] && d[v][]==p.w+w){ //更新次短路长数
dp[v][]+=dp[p.v][p.mark];
}
}
}
}
int main(void)
{
int u,v,w;
while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)
{
for(int i=;i<n;i++)
V[i].clear();
for(int i=;i<m;i++){
scanf("%d%d%d",&u,&v,&w);
V[u].push_back(edge(v,w));
}
dijkstra(s);
printf("%d %d\n",d[e][],dp[e][]);
}
return ;
}
hdu 3191 How Many Paths Are There (次短路径数)的更多相关文章
- hdu 3191 How Many Paths Are There
http://acm.hdu.edu.cn/showproblem.php?pid=3191 这道题求次短路经和路径数 #include <cstdio> #include <cst ...
- 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 ...
- 【LeetCode每天一题】Unique Paths(唯一的路径数)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The ...
- HDU 1688 Sightseeing 【输出最短路+次短路条数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...
- hdu 3191 次短路的长度和个数
http://acm.hdu.edu.cn/showproblem.php?pid=3191 求次短路的长度和个数 相关分析在这里http://blog.csdn.net/u012774187/art ...
- HDU 6181:Two Paths(次短路)
Two Paths Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others) Total S ...
- 【hdu 6181】Two Paths
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6181 [题意] 让你求从1到n的次短路 [题解] 模板题; 因为点可以重复走; 则一定会有次短路. di ...
- HDU 3191 次短路长度和条数
http://www.cnblogs.com/wally/archive/2013/04/16/3024490.html http://blog.csdn.net/me4546/article/det ...
- HDU 6181:Two Paths(A* + SPFA)
题目链接 题意 给出n个点m条边的无向图,求次短路. 思路 和 POJ 2449 类似,只不过大小要开成long long. #include <bits/stdc++.h> using ...
随机推荐
- 北京Uber优步司机奖励政策(12月23日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月27日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 长沙Uber优步司机奖励政策(12月14日到12月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- spring源码-bean之增强初始化-3
一.ApplicationContext的中文意思是“应用上下文”,它继承自BeanFactory接口,除了包含BeanFactory的所有功能之外,在国际化支持.资源访问(如URL和文件).事件传播 ...
- zookeeper的图形化展示
1.ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供 ...
- 利尔达NB-IOT模组Coap数据AT+NMGS发送时返回-513的原因
1. 利尔达NB-IOT模组使用AT+NMGS发送数据,返回-513的问题,大致有3种可能性,在硬件上,模组的射频电路分为A型和B型模组,所以烧写固件的时候,也要分为A和B型固件,如果烧写反了,那么R ...
- 一个只有十行的精简MVVM框架(下篇)
本文来自网易云社区. 让我们来加点互动 前面学生信息的身高的单位都是默认m,如果新增一个需求,要求学生的身高的单位可以在m和cm之间切换呢? 首先需要一个变量来保存度量单位,因此这里必须用一个新的Mo ...
- python generator
def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index] for char in reverse('g ...
- Python类对象
python类对象 python类对象支持两种操作:属性引用和实例化. 属性引用 使用 Python 中所有属性引用所使用的标准语法: obj.name. 有效的属性名称是类对象被创建时存在于类命名空 ...
- C 计算数字的位数循环
#include <stdio.h> int main(int argc, char **argv) { // int x; int n=0; scanf("%d",& ...