从点N到1的最短路

*记得无向图两个方向都要建边就好了……

以及多组数据= =

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define SZ 4005
#define INF 1e9+10
int head[SZ],nxt[SZ],tot = ;
struct edge
{
int t,d;
}l[SZ];
void build(int f,int t,int d)
{
l[++tot] = (edge){t,d};
nxt[tot] = head[f];
head[f] = tot;
}
queue<int> q;
bool use[SZ];
int dist[SZ];
int spfa(int s, int e)
{
memset(dist, , sizeof(dist));
memset(use, , sizeof(use));
use[s] = ;
dist[s] = ;
q.push(s);
while(!q.empty())
{
int u = q.front(); q.pop();
use[u] = ;
for(int i = head[u];i;i = nxt[i])
{
int v = l[i].t;
if(dist[v] > dist[u] + l[i].d)
{
dist[v] = dist[u] + l[i].d;
if(!use[v])
use[v] = , q.push(v);
}
}
}
return dist[e];
}
int main()
{
int T, N;
while(scanf("%d %d", &T, &N) != EOF)
{
memset(head, , sizeof(head));
tot = ;
for(int i = ; i < T; i++)
{
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
build(x, y, z);
build(y, x, z);
}
printf("%d\n", spfa(N, ));
}
return ;
}

最短路 || POJ 2387 Til the Cows Come Home的更多相关文章

  1. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  2. 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 ...

  3. POJ 2387 Til the Cows Come Home

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

  4. 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 ...

  5. 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 ...

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

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

  7. POJ 2387 Til the Cows Come Home Dijkstra求最短路径

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

  8. 怒学三算法 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 ...

  9. POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)

    原题链接:Til the Cows Come Home 题目大意:有  个点,给出从  点到  点的距离并且  和  是互相可以抵达的,问从  到  的最短距离. 题目分析:这是一道典型的最短路径模版 ...

随机推荐

  1. PhpStorm比较高级的一些设置

    开始使用phpstorm工具,总体感觉还是不错的.有点不舒服的就是他占用内存比较高,反应速度有时会卡,这跟他的功能强大有关系.有些功能对于 我来说是不怎么需要的,比如自动保存功能,会频繁的保存一些文件 ...

  2. 一个不当使用fclose引发的异常

    最近服务器上一个后台传输文件的服务,经常会报出异常来,只能强行终止并重启. 昨天刚好有空,现场抓了一下dump,再把程序扔到IDA里看了一下,很快就找出原因了,原来是调用fclose时出错的. 使用C ...

  3. UVa 1644 Prime Gap (水题,暴力)

    题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...

  4. 与adb相关的问题,比如掉线问题、Android Studio 提示Session 'app':Error Installing APK、找不到设备

    这一篇帖子 会写的比较简单 不过相信大家也可能遇到这些问题   为了怕自己忘记  记录下来  顺便也和大家一起分享 描述:在一些机型上安装软件 提示卸载原先的软件  但是又安装不上新软件 DELETE ...

  5. CocoaPods(第三方类库管理工具)

    iOS安装CocoaPods详细过程  一.简介 什么是CocoaPods CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods” ...

  6. 51nod 1489 蜥蜴和地下室(dp)

    传送门 题意 分析 dp[12][20][20][20]; // dp[a][b][c][d]第a个弓箭手面临第a-1.a.a+1个弓箭手的生命值分别为b.c.d的状态 转移巧妙,需注意 trick ...

  7. Codeforces 550B 【暴力】

    题意: 有n个数字, 要求在这n个数中选出至少两个数字, 使得它们的和在L,R之间,并且最大的与最小的差值要不小于x 思路: 撒比了啊... 根据状态的话一共也就是2^15-直接暴力,二进制的小魅力还 ...

  8. Unity3D将来时:IL2CPP(上)

    http://inpla.net/thread-8197-1-1.html Unity3D将来时:IL2CPP(上) (注:本文详细的讲述了C#,Mono,.Net, IL等Unity使用到的概念,如 ...

  9. GCD = XOR(GCD XOR )

    首先没看懂XOR(于是百度了一下):异或,英文为exclusive OR,或缩写成xor.同时还有一个OR,于是一起看了一眼: 大意: 输入一个整数n,在1~n内,有多少对整数(a,b)满足GCD(a ...

  10. Python标准库 datetime

    >>> import datetime >>> now = datetime.datetime.now() >>> now datetime.da ...