POJ 2387 Til the Cows Come Home
来源:互联网 发布:电视观众收视数据分析 编辑:程序博客网 时间:2024/05/17 03:59
农夫囧的头号后宫母牛背时迷路了。给定一张无向图,背时在标号为n的节点。问背时回标号1的节点的最短路有多长。
最短路,用dijkstra的话会比较麻烦,因为这题可能有重边。所以就要用到传说中号称快速求最短路的SPFA算法。
诶。。其实是第一次写spfa,把模板放在这里,以后方便复习。。
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;struct edge{ int u; int v; int w;};edge e[5000];vector<int> v[2100];queue<int> q;bool inque[2100];int le,dist[2100];int spfa(int sta,int tar){ int t,i,te; while (!q.empty()) q.pop(); memset(inque,false,sizeof(inque)); memset(dist,127,sizeof(dist)); q.push(sta); inque[sta]=true; dist[sta]=0; while (!q.empty()) { t=q.front(); q.pop(); inque[t]=false; for (i=0; i<v[t].size(); i++) { te=v[t][i]; if (dist[e[te].v] > dist[t]+e[te].w) { dist[e[te].v]=dist[t]+e[te].w; if (inque[e[te].v] == false) { inque[e[te].v]=true; q.push(e[te].v); } } } } return dist[tar];}int main(){ int n,t,i,j,t1,t2,t3; scanf("%d%d",&t,&n); le=0; for (i=1; i<=n; i++) v[i].clear(); for (i=0; i<t; i++) { scanf("%d%d%d",&t1,&t2,&t3); v[t1].push_back(le); e[le].u=t1; e[le].v=t2; e[le].w=t3; le++; v[t2].push_back(le); e[le].u=t2; e[le].v=t1; e[le].w=t3; le++; } printf("%d\n",spfa(1,n));}
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- Poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ-2387-Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- poj 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- POJ 2387 Til the Cows Come Home
- JavaScript————基础1
- snoopy(强大的PHP采集类) 实例应用
- C语言中可变参数的用法——va_list、va_start、va_arg、va_end参数定义
- 归一化RGB
- matlab中调用c/c++程序
- POJ 2387 Til the Cows Come Home
- 九度1050 完数
- TLD取经之路(1)--VS2008,MATLB2010B与OPENCV2.2
- 数据库出XXX.exe,内存位置YYY处的_com_error。没有验证过,先记录下来。
- TLD取经之路(2)-- 初窥门径:运行DEMO
- 别人总结的OpenTLD资源,MARK
- TLD取经之路(3)-- 始于足下--tldDemo.m tldInit.m
- ASP.NET系统对象与状态管理
- matlab的cell数组