题目链接:http://poj.org/problem?id=2387

题目大意;

题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离。。

思路:dijkstra求单源最短路,但是要注意判重。
 
#include <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
#define min(a,b) a<=b?a:b
int vis[], dis[]; //dis[j]表示起点到当前点的最短距离
int n, t;
int map[][]; void dij()
{
int i, j, cur, k;
memset(vis, , sizeof(vis));
for (i = ; i <= n; i++)(i == ) ? (dis[i] = ) : (dis[i] = inf);
cur = ;
for (i = ; i < n; i++) //循环n次,每次挑选没走过的到起点距离最短的点
{
vis[cur] = ;
for (j = ; j <= n; j++)
{
if (vis[j] == )
dis[j] = min(dis[j], map[cur][j] + dis[cur]); //更新每个没走过的点,到起点的最短距离
} //选择到起点距离最短的点
int g = inf; int x = ;
for (j = ; j <= n; j++)
{
if (dis[j] <= g && !vis[j])
{
g = dis[j];
x = j;
}
}
cur = x;
}
printf("%d\n", dis[n]); //输出终点到起点的最短距离
} int main()
{
int i, j, a, b, c;
while (scanf("%d%d", &t, &n) != EOF)
{
memset(map, inf, sizeof(map));
for (i = ; i <= t; i++)
{
scanf("%d%d%d", &a, &b, &c); //去除重边的情况
if (c < map[a][b])
map[a][b] = map[b][a] = c;
}
dij();
}
return ;
}

2018-04-01

POJ2387 Til the Cows Come Home 【Dijkstra】的更多相关文章

  1. POJ2387 Til the Cows Come Home【Kruscal】

    题目链接>>> 题目大意: 谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大 解题思路: 最 ...

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

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

  3. POj2387——Til the Cows Come Home——————【最短路】

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

  4. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  5. poj2387 Til the Cows Come Home(Dijkstra)

    题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...

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

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  7. (Dijkstra) POJ2387 Til the Cows Come Home

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

  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 【最短路SPFA】

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

随机推荐

  1. Modbus库开发笔记之一:实现功能的基本设计

    Modbus作为开放式的工业通讯协议,在各种工业设备中应用极其广泛.本人也使用Modbus通讯很多年了,或者用现成的,或者针对具体应用开发,一直以来都想要开发一个比较通用的协议栈能在后续的项目中复用, ...

  2. Ubuntu16.04安装JDK8与Tomcat7

      本篇教程在示例步骤中使用了以下版本的软件.操作时,请您以实际软件版本为准. 操作系统:Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-105-generic x86_64) ...

  3. spring springMvc spring-boot spring-cloud分别是什么

    本文来源于:克己习礼成仁   的<spring springMvc spring-boot spring-cloud分别是什么> 什么是spring 关于spring的定义无论是从官方还是 ...

  4. Confluence 6 用户提交的备份和恢复脚本

    下面的代码是用户提交的,在使用的时候需要小心,因为 Atlassian 不提供这些代码的技术支持.如果你在使用或者修改这些代码的时候有任何问题,请粘贴到 post them to Atlassian ...

  5. UserNotifications ios10 通知使用

    通知在ios10 中推荐使用 导入  import UserNotifications  头文件 if #available(iOS 10.0, *) { UNUserNotificationCent ...

  6. 10,关于在vs2017中的netcore项目使用BundlerMinifier 配置问题

    查阅资料 https://github.com/madskristensen/BundlerMinifier/issues/230 今天下载了vs2017 rc 创建了个netcore的web项目,突 ...

  7. 《剑指offer》栈的插入弹出序列

    本题来自<剑指offer> 栈的插入弹出序列 题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2, ...

  8. java----static关键字(包括final)

    static修饰字段: 使用static关键字修饰一个字段:声明的static变量实际上就是一个全局变量 使用static关键字修饰一个方法:可以直接使用类调用方法,和对象没有关系了 使用static ...

  9. poj1155 依赖背包

    /* 依赖背包 dp[i][j]表示i结点为根的树选择j个用户时的最大剩余费用 即背包容量是j,价值是最大费用 */ #include<iostream> #include<cstr ...

  10. ajax--参数映射方式实现阴影效果

    注:通过json对象的方式传递参数,参数具体信息由json对象来封装,参数封装到对象中再进行映射(参数映射) shadow.js //使用参数映射方式实现参数设置/* option:参数对象,封装所有 ...