spfa+队列+邻接表POJ3159
来源:互联网 发布:黑客破解软件授权 编辑:程序博客网 时间:2024/06/10 08:56
原题http://poj.org/problem?id=3159
#include <stdio.h>#include <string.h>#define VM 300005#define EM 1500005#define inf 0x3fint head[VM],ep;struct edge{ int v,w,next;}e[EM];void addedge(int cu,int cv,int cw){ ep ++; e[ep].v = cv; e[ep].w = cw; e[ep].next = head[cu]; head[cu] = ep;}void spfa (int n){ int vis[VM],queue[VM],dis[VM]; memset (vis,0,sizeof(vis)); memset (dis,0x3f,sizeof(dis)); dis[1] = 0; int front = -1,rear = 0; queue[rear++] = 1; vis[1] = 1; while (front != rear) { front = (front+1)%(n+1); int u = queue[front]; vis[u] = 0; for (int i = head[u];i != -1;i = e[i].next) { if (dis[e[i].v] > dis[u]+e[i].w){dis[e[i].v] = dis[u]+e[i].w;if (!vis[e[i].v]){vis[e[i].v] = 1;queue[rear] = e[i].v;rear = (rear+1)%(n+1);}} } } printf ("%d\n",dis[n]);}int main (){ int n,m,v1,v2,cost; ep = 0; memset (head,-1,sizeof(head)); scanf ("%d%d",&n,&m); while (m--) { scanf ("%d%d%d",&v1,&v2,&cost); addedge (v1,v2,cost); } spfa (n); return 0;}//这题用这种方法会超时,要用栈
0 0
- spfa+队列+邻接表POJ3159
- spfa+栈+邻接表模板POJ3159
- POJ3159---Candies(最短路:spfa+栈or队列)(邻接表)
- poj3159差分约束+栈实现的spfa+邻接链表 差分约束
- POJ3159::SPFA
- POJ 1724 邻接表+优先队列+spfa()
- poj 1724 ROADS 邻接表+优先队列+spfa()
- spfa+邻接表
- SPFA 邻接表
- SPFA+邻接表
- 邻接表的spfa
- cug1697 spfa+邻接表
- [模板]spfa+邻接表
- SPFA 邻接表模式
- poj3159(spfa+栈)
- poj3159 Candies(SPFA+stack)
- poj3159(spfa)
- POJ3159 Candies(差分约束 PS:此题spfa+队列过不了,spfa + stack才能过)
- openstack 虚拟机获取网络ip-记录一下过程
- spfa+栈+邻接表模板POJ3159
- FME 破解
- poj1019
- 如何使用Android MediaStore裁剪大图片
- spfa+队列+邻接表POJ3159
- 有N个标注,把这些标注放在屏幕中间显示
- VS2012 自己 Cocos2dx 的安装配置问题
- eclipse导入android工程@override报错
- 最小生成树邻接表模板
- webView loadData 中文乱码问题
- Cocos2d-x 3.0多线程异步资源加载
- 1407231857-hd-Problem A .cpp
- hdu 2196 树形dp