hdu1874
来源:互联网 发布:广联达软件官方下载 编辑:程序博客网 时间:2024/06/10 18:39
求最短路,且路可能不存在
0x3f3f3f3f大但是不至于太大。。
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#define inf 0x3f3f3f3f#define maxn 210#define maxm 1010using namespace std;int first[maxn];//某节点指向的第1条边int e;//总边数int v[maxm];//边指向的终点int next[maxm];//某条边的下一条int cost[maxm];//某条边的权值int d[maxn],visit[maxn],tim[maxn];void add_edge(int a,int b,int c){ v[e]=b; next[e]=first[a]; first[a]=e; cost[e]=c; e++;}void spfa(int s){ queue<int>q; memset(d,0x3f,sizeof(d)); memset(visit,0,sizeof(visit)); d[s]=0; visit[s]=1; q.push(s); while(!q.empty()) { int num=q.front();//记录当前编号 q.pop(); visit[num]=0;//已出队的要再改标记 for(int i=first[num];i!=-1;i=next[i]) { if(d[v[i]]>d[num]+cost[i])//更新 { d[v[i]]=d[num]+cost[i]; if(!visit[v[i]]) { q.push(v[i]); visit[v[i]]=1; } } } }}int main(){ int n,m,s,t,a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { e=0; memset(first,-1,sizeof(first)); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); add_edge(a,b,c); add_edge(b,a,c); } scanf("%d%d",&s,&t); spfa(s); if(d[t] != inf) printf("%d\n",d[t]); else printf("-1\n"); } return 0;}
0 0
- hdu1874
- hdu1874
- hdu1874
- HDU1874
- hdu1874
- HDU1874
- hdu1874
- hdu1874
- hdu1874
- hdu1874
- hdu1874
- hdu1874
- hdu1874
- hdu1874(dijkstra)
- hdu1874 dijkstra
- hdu1874 dijkstra
- hdu1874畅通工程续
- hdu1874最短路
- 绝不在构造和析构过程中调用virtual函数 UC2014年实习生笔试题
- protel component not found
- 一张浏览器加载时序图。以后有时间再分析
- java for循环注意事项
- 关闭VirtualBox虚拟机的时钟同步
- hdu1874
- hdu1518
- ubuntu环境下重新编译内核
- Mustache php 版开源项目 流程 性能分析 以及优化
- hd 1996 汉诺塔VI
- 创建流水码
- MyEclipse启动时出现异常:An internal error occurred during: "Updating indexes".
- ceph的librados api解释
- 21 网络编程