poj 3255
来源:互联网 发布:zcash挖矿软件 编辑:程序博客网 时间:2024/05/17 01:39
次短路练习
spfa+A*
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#define MAX 1e9#define MAXE 200100using namespace std;struct edge{ int v,w,n;}e[MAXE];int n,m;int head[10000],num,dis[10000],in[10000],rdis[10000];void add(int a,int b,int c){ e[num].v=b; e[num].w=c; e[num].n=head[a]; head[a]=num; num++;}void SPFA(int k,int d[]){ for(int i=1;i<=n;i++) d[i]=MAX; d[k]=0; memset(in,0,sizeof in); queue <int> q; q.push(k); in[k]=1; while(!q.empty()) { int t; t=q.front(); q.pop(); in[t]=0; for(int i=head[t];i!=-1;i=e[i].n) { int v=e[i].v; int w=e[i].w; if(d[v]>d[t]+w) { d[v]=d[t]+w; if(in[v]==0) { in[v]=1; q.push(v); } } } }}int main(){// freopen("in","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { memset(head,-1,sizeof head); num=0; for(int i=0;i<m;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(a,b,c); add(b,a,c); } SPFA(1,dis); SPFA(n,rdis); int ans=MAX; for(int i=1;i<=n;i++) for(int j=head[i];j!=-1;j=e[j].n) { int v=e[j].v; int w=e[j].w; int temp=dis[i]+rdis[v]+w; if(temp>dis[n]&&ans>temp) ans=temp; } printf("%d\n",ans); } return 0;}
0 0
- POJ 3255
- poj 3255
- poj 3255
- poj 3255
- POJ 3255
- POJ 3255
- poj 3255
- poj 3255
- poj 3255
- poj 3255
- POJ 3255
- poj 3255
- POJ 3255
- poj 3255 Roadblocks
- poj 3255 Roadblocks
- poj 3255 求次大最短路
- POJ-3255-Roadblocks
- POJ 3255 Roadblocks
- MFC学习笔记(3)—— 消息(2)
- Cocos2d-x 开发神器cococreator使用介绍
- protobuf数据类型
- LeetCode - Two Sum
- 百练2755 神奇的口袋 【深搜】or【动规】or【普通递归】or【递推】
- poj 3255
- UVA10387 Billiard
- [转]Android蓝牙开发浅谈
- Github新手入门,简单使用方法!
- VBA Excel值数据替换
- 味儿
- ZOJ 1012 Mainframe
- uva 1368 - DNA Consensus String
- 邮件接收pop3和imap