1752: [Usaco2005 qua]Til the Cows Come Home (SPFA)
2014-07-12 22:48
405 查看
#include<iostream> #include<cstring> #include<cstdio> #define inf 0x7fffffff using namespace std; inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct edge{ int to,next,v; }e[40001]; int n,m,cnt,head[10001],dis[10001]; void ins(int u,int v,int w){ e[++cnt]=(edge){v,head[u],w};head[u]=cnt; e[++cnt]=(edge){u,head[v],w};head[v]=cnt; } void spfa() { int q[10001], t = 0, w = 0; bool inq[10001]; memset(inq, 0, sizeof (inq)); memset(dis, 127, sizeof (dis)); dis[1]=0;q[0]=1;inq[1]=1; while (t <= w) { int now = q[t++]; for (int i = head[now]; i; i = e[i].next) { if (dis[now] + e[i].v < dis[e[i].to]) { dis[e[i].to] = dis[now] + e[i].v; if (!inq[e[i].to]) { q[++w] = e[i].to; inq[e[i].to] = 1; } } } inq[now] = 0; } } int main(){ m=read();n=read(); for(int i=1;i<=m;i++){ int a=read(),b=read(),c=read(); ins(a,b,c); } spfa(); printf("%d",dis ); return 0; }
相关文章推荐
- bzoj1752 [Usaco2005 qua]Til the Cows Come Home
- bzoj1752 [Usaco2005 qua]Til the Cows Come Home
- 3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
- POJ.2387 Til the Cows Come Home (SPFA)
- bzoj 3386: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
- Til the Cows Come Home(最短路(单源SPFA))
- POJ-2387-Til the Cows Come Home(SPFA)
- POJ_2387_Til the Cows Come Home(USACO 2004 November)_最短路径
- Til the Cows Come Home 【SPFA】 【djk】
- POJ 2387 Til the Cows Come Home(spfa)
- poj 2387 Til the Cows Come Home 最短路(贝尔曼,迪杰斯特拉,spfa)
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
- poj 2387 Til the Cows Come Home多种用法详解【dij vector||spfa||bellman_ford】
- poj 2387 Til the Cows Come Home spfa基础题,入门,我的第一个
- [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家
- POJ 2387 Til the Cows Come Home (Spfa)
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
- Til the Cows Come Home POJ - 2387 单源最短路 SPFA实现
- POJ Til the Cows Come Home【dijkstra+spfa】
- POJ 2387 Til the Cows Come Home (单源最短路SPFA解法)