从点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. python学习笔记1-基础语法

    1 在3版本中print需要加上括号2 多行语句:用\连接 item_one=1 item_two=2 item_three=3 total = item_one + \ item_two + \ i ...

  2. DB Link 去除域名

    1.查看global_name的设置 SQL> show parameters global_name; NAME                                 TYPE    ...

  3. ORACLE PL/SQL 实例精解之第三章 PL/SQL中的SQL

    3.1 在PL/SQL中使用DML 在PL/SQL语块中,两种变量赋值a. := 初始化.b. select into语法,PL/SQL语块的声明部分被声明的变量,后期可以使用选择语句进行赋值. 3. ...

  4. 洛谷P2569 [SCOI2010]股票交易(单调队列)

    传送门 惭愧……这种题目都没看出来…… 首先,我们用$dp[i][j]$表示在第$i$天,手上有$j$股时的最大收益 第一,我们可以直接买股票,即$dp[i][j]=-j*AP_i$,这个直接计算即可 ...

  5. CF580D Kefa and Dishes 【状压dp】By cellur925

    题目传送门 友情链接:new2zydalao%%%  一篇优秀的状压文章 题目大意:$n$个菜有$k$个规则,如果kefa在吃完第$xi$个菜之后吃了第$yi$个菜(保证$xi$.$yi$不相等), ...

  6. Luogu P1613跑路【倍增】By cellur925

    题目传送门 开始的思路:直接跑一遍最短路,得到最短路的那个值.然后把那个值进行一下二进制拆分,看能拆几次.(可能是受到了刚做过的题影响) ;i>=;i--) { <<i)>qw ...

  7. Django Views and URLconfs

    碧玉妆成一树高,万条垂下绿丝绦. 不知细叶谁裁出,二月春风似剪刀. 原文尽在:http://djangobook.com/ 转载请注明出处:http://www.cnblogs.com/A-FM/p/ ...

  8. IO流案例:1.复制多级文件夹 2.删除多级文件夹

    package copy; /* 需求:复制多级文件夹 复制d:\\itcast(包含文件和子文件夹)到模块目录下 分析: d:\\itcast a.txt b.txt javaweb a.xml b ...

  9. macOS 设置Root密码

    用管理员帐号进入Terminal: 1) 输入:sudo passwd root ,回车: 2) 输入新的root密码: 3) 输入:su : 4) 输入新密码: 这样就进入到root帐号了.

  10. Python标准库 os

    (掌握os模块,你需要了解Linux或类Unix系统下常用命令的操作) os.name  指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix' ...