hdu 3191 How Many Paths Are There
http://acm.hdu.edu.cn/showproblem.php?pid=3191
这道题求次短路经和路径数
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define maxn 2000
using namespace std;
const int inf=<<;
struct edge
{
int v,w;
}; struct node
{
int d,v;
int mark;
bool operator < (const node &a)const
{
if(d!=a.d)
return d>a.d;
return v>a.v;
}
}; vector<edge>edges[maxn];
int dis[maxn][];
int vis[maxn][];
int path[maxn][];
int n,m,a,b,c,s1,f;
node st; void inti()
{
for(int i=; i<=n; i++)
{
dis[i][]=dis[i][]=inf;
}
memset(path,,sizeof(path));
memset(vis,false,sizeof(vis));
} void dijkstra(int s,int e)
{
inti();
priority_queue<node>q;
dis[s][]=;
path[s][]=;
memset(vis,false,sizeof(vis));
st.d=;
st.v=s;
st.mark=;
q.push(st);
while(!q.empty())
{
node st1=q.top();
q.pop();
if(vis[st1.v][st1.mark]) continue;
vis[st1.v][st1.mark]=true;
for(int i=; i<(int)edges[st1.v].size(); i++)
{
int v1=edges[st1.v][i].v;
int w1=edges[st1.v][i].w;
if(!vis[v1][]&&st1.d+w1<dis[v1][])
{
if(dis[v1][]!=inf)
{
dis[v1][]=dis[v1][];
path[v1][]=path[v1][];
st.d=dis[v1][];
st.v=v1;
st.mark=;
q.push(st);
}
dis[v1][]=st1.d+w1;
path[v1][]=path[st1.v][st1.mark];
st.v=v1;
st.mark=;
st.d=dis[v1][];
q.push(st);
}
else if(!vis[v1][]&&st1.d+w1==dis[v1][])
{
path[v1][]+=path[st1.v][st1.mark];
}
else if(!vis[v1][]&&st1.d+w1<dis[v1][])
{
dis[v1][]=st1.d+w1;
path[v1][]=path[st1.v][st1.mark];
st.d=dis[v1][];
st.v=v1;
st.mark=;
q.push(st);
}
else if(!vis[v1][]&&st1.d+w1==dis[v1][])
{
path[v1][]+=path[st1.v][st1.mark];
}
}
}
} int main()
{
while(scanf("%d%d%d%d",&n,&m,&s1,&f)!=EOF)
{
inti();
for(int i=; i<=n; i++) edges[i].clear();
for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
edge m1;
m1.v=b;
m1.w=c;
edges[a].push_back(m1);
}
dijkstra(s1,f);
printf("%d %d\n",dis[f][],path[f][]);
}
return ;
}
hdu 3191 How Many Paths Are There的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
随机推荐
- windows bat命令编写大全
1 echo 和 @ @ #关闭单行回显echo off #从下一行开始关闭回显 @echo off #从本行开始关闭回显.一般批处理第一行都是这个 echo on #从下一行开始打开回显 ec ...
- 使用QtScript库解析Json数组例子
本文转载自:http://blog.sina.com.cn/s/blog_671732440100uwxh.html 使用qtscipt库解析json数组首先在工程文件中加 QT += ...
- 【转】Ubuntu 修改hosts
原文网址:http://l.14551.org/2009/12/2166 Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改 ...
- C(m,n)%P
program1 n!%P(P为质数) 我们发现n! mod P的计算过程是以P为周期的的,举例如下: n = 10, P = 3 n! = 1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 ...
- PHP关于时区问题
最近在学习PHP过程中发现PHP中的格式化时间戳比北京时间晚了8个小时,上网搜索发现原来是时区不对,解决办法是: 1.永久修改 更改php.ini文件中的data.tim ...
- Day56
今天干啦啥呢 早上七点起来 天气冷了真起不来啊 再坚持坚持就好了 今天上午九点开始考数据库 我去 大学四年第一次感觉到这样的爽 作弊的爽啊 也没有完全的作弊啦 是开卷考试,反正做的很顺利了. 我和胡 ...
- C# - 创建List属性的简单方法
不用担心List没有创建问题. private ObservableCollection<EquipmentItem> _optionalCollection; public Observ ...
- Firefox历史版本下载
http://ftp.mozilla.org/pub/firefox/releases/ http://ftp.mozilla.org/pub/firefox/releases/47.0.1/
- iOS 语音识别使用讯飞报错
You must rebuild it with bitcode enabled(Xcode setting ENABLE_BITCODE), obtain an updated library fr ...
- pyqt例子下拉列表
#!/usr/bin/env python # -*- coding: utf-8 -*- from PyQt4.QtCore import Qt from PyQt4.QtGui import QC ...