从点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. linux下ping不通的解决方法

    转自:https://blog.csdn.net/weixin_33400820/article/details/80227702 今天在做练习的时候,发现如何都无法ping通外网,在经过各种网络求助 ...

  2. VMware Workstation安装centos 6.5详细步骤

    转自“http://blog.51cto.com/12496630/2058386” 22.选择分区了,centos新版中使用lvm来分区,我不用过分去计算分区大小,这个模式可以允许用户以后动态调整分 ...

  3. VC++、MFC

    VC++.MFC最好的开源项目 介绍:介绍一下用VC++/MFC写的最好的开源项目. Sourceforge.net中有许多高质量的VC++开源项目,我列举了一些可以作为VC++程序员的参考. 正文: ...

  4. 聊聊Web App、Hybrid App与Native App的设计差异(转)

    目前主流应用程序大体分为三类:Web App.Hybrid App. Native App. 一.Web App.Hybrid App.Native App 纵向对比 首先,我们来看看什么是 Web ...

  5. hihoCoder扩展欧几里得

    #include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  6. bzoj 4240: 有趣的家庭菜园【树状数组+贪心】

    以为是逆序对数-- 实际上,原数组移动若干次后我们会得到一个新的下标序列,需要的移动次数是这个新下标序列的逆序对数 然后我们要让这个最小,考虑贪心先按h把下标排一遍序,然后每次询问到一种值的时候,对每 ...

  7. CF1060E Sergey and Subway(点分治)

    给出一颗$N$个节点的树,现在我们**在原图中**每个不直接连边但是中间只间隔一个点的两个点之间连一条边. 比如**在原图中**$u$与$v$连边,$v$与$w$连边,但是$u$与$w$不连边,这时候 ...

  8. 纯JS阻止浏览器默认滚动事件,实现自定义滚动方法

    首先该方法兼容IE7+以上浏览器,可以实现页面上下滚动,而且也可以实现页面左右滚动,每次滚动的距离为屏幕的大小,滚动为加速滚动 javaScript代码如下: //滚动实现方法,使用鼠标滚轮每次滚动浏 ...

  9. 第十八篇 .NET高级技术之Linq与EF Code-First Fluent API基础讲解

    1.FluentApi简介 在这里提供了一个fluentapi基础的DEMO然后咱们在进一步的学习,直接上干货. 第一步在数据库创建一个表:person 第二步:新建控制台程序FluentAPI 第三 ...

  10. 关于asp.net网址出现乱码问题的解决方法

    背景: asp.net项目,C#,VS2010,.netframework 4.0 创建之初,没有任何问题,随着项目文件的增多,不免很多问题会出现, 最近就莫名其妙的发现我的项目网址多了一段乱码,很是 ...