裸的最短路,试一下刚看的spfa,虽然没有看代码,不过明白了大致的思想,先写一下试试吧,而且是个稀疏图,应该会很快吧。

SPFA

算法采用图的存储结构是邻接表,方法是动态优化逼近法。算法中设立了一个先进先出的队列Queue用来保存待优化的顶点,优化时从此队列里顺序取出一个点w,并且用w点的当前路径D[W]去优化调整其它各点的路径值D[j],若有调整,即D[j]的值改小了,就将J点放入Queue队列以待继续进一步优化。反复从Queue队列里取出点来对当前最短路径进行优化,直至队空不需要再优化为止,此时D数组里就保存了从源点到各点的最短路径值

///////////////////////////////////////////////////////////////////////////

竟然一遍过......好神奇的感觉,写起来也简单了不少

#include<algorithm>
#include<queue>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std; const int maxn = ;
const int oo = 0xfffffff; struct node
{
    int y, len;
    node(int y, int len):y(y), len(len){}
};
vector<node> G[maxn];
int v[maxn]; void Spfa(int s)
{
    queue<int> Q;     Q.push(s);     while(Q.size())
    {
        s = Q.front();Q.pop();         int len = G[s].size();
        for(int i=; i<len; i++)
        {
            node q = G[s][i];
            if(v[s]+q.len < v[q.y])
            {
                v[q.y] = v[s]+q.len;
                Q.push(q.y);
            }
        }
    }
} int main()
{
    int T, N;     while(scanf("%d%d", &T, &N) != EOF)
    {
        int i, a, b, len;         for(i=; i<T; i++)
        {
            scanf("%d%d%d", &a, &b, &len);
            G[a].push_back(node(b, len));
            G[b].push_back(node(a, len));
        }         for(i=; i<=N; i++)
            v[i] = oo;
        v[] = ;         Spfa();         printf("%d\n", v[N]);         for(i=; i<=N; i++)
            G[i].clear();
    }     return ;

}

A - Til the Cows Come Home的更多相关文章

  1. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  2. Til the Cows Come Home(最短路)

    Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  3. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  4. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  5. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted ...

  6. POJ 2387 Til the Cows Come Home (最短路 dijkstra)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...

  7. 3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家

    3386/1752: [Usaco2004 Nov]Til the Cows Come Home 带奶牛回家 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit ...

  8. (Dijkstra) POJ2387 Til the Cows Come Home

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 81024   Accepted ...

  9. POJ 2387 Til the Cows Come Home 【最短路SPFA】

    Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...

  10. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

随机推荐

  1. MySQL性能状态查看方式

    1. QPS(每秒Query量) QPS = Questions(or Queries) / seconds mysql > show global status like 'Question% ...

  2. throw 导致 Error C2220, wraning C4702错误

    今天在程序加了一个语句,发现报 Error C2220, Wraning C4702错误 查询Wraning C4702 ,[无法访问的代码] 由于为 Visual Studio .NET 2003 ...

  3. c读写文件相关

    1.打开文件: 函数原型: FILE * fopen(const char * path,const char * mode); 返回值: 文件顺利打开后,指向该流的文件指针就会被返回.如果文件打开失 ...

  4. 入门4:PHP 语法基础1

    一.PHP标记符 1.PHP页面中 以<?php  开头, ?>结尾,纯PHP页面结尾可以不写?> 2.在HTML页面插入PHP代码,必须有?>结尾.代码如下: <!DO ...

  5. ThinkPHP接入支付宝支付功能

    最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能.这里我用的是即时到帐的接口,具体实现的步骤如下: 一.下载支付宝接口包 下载地址:https://b.alipay.com/o ...

  6. ueditor的过滤、转义、格式丢失问题

    1. 过滤 http://www.cnblogs.com/Olive116/p/3464495.html 2. 转义 http://segmentfault.com/q/101000000048928 ...

  7. #module-django.db.models

    Models A model is the single, definitive source of information about your data. It contains the esse ...

  8. IOS 企业版证书($299)In-House方式发布指南

    一.明确几个概念 1.企业版IDP:即iOS Development Enterprise Program.注意是$299/Year那种,并不是$99/Year的那种. 2.In House:是只企业 ...

  9. uboot使用tftp下载时出现“checksum bad”问题原因分析

    一.问题 二.原因分析 你的虚拟机是不是这样设置的呢? 如果是的话,请看下边的解释: 使用NAT模式,就是让虚拟系统借助NAT(网络地址转换)功能,通过宿主机器所在的网络来访问公网.也就是说,使用NA ...

  10. python中对文件、文件夹的操作需要涉及到os模块和shutil模块。

    创建文件:1) os.mknod("test.txt") 创建空文件2) open("test.txt",w) 直接打开一个文件,如果文件不存在则创建文件 创建 ...