前两天自学了一点点最短路..看起来很简单的样子...

就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~

松弛虽然会但是用的十分之不熟练...

代码~

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int tance[1005][1005];
bool vis[1005];
int dis[1005];
int t,n;
void sc()
{
for(int i=1;i<=n;i++)
vis[i]=true;
vis[1]=false;
for(int i=2;i<=n;i++)
{
dis[i]=tance[1][i];
}
dis[1]=0;
for(int i=1;i<=n-1;i++)
{
int minwhere;
int min=999999;
for(int k=2;k<=n;k++)
{
if(vis[k]==true&&dis[k]<min)
{
min=dis[k];
minwhere=k;
}
}
vis[minwhere]=false;
for(int k=2;k<=n;k++)
{
if(vis[k]==true)
{
if(dis[k]>dis[minwhere]+tance[minwhere][k])
{
dis[k]=dis[minwhere]+tance[minwhere][k];
}
}
} }
return ;
}
int main(){
while(~scanf("%d%d",&t,&n))
{
memset(tance,105,sizeof(tance));
int a,b,c;
for(int i=1;i<=t;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(tance[a][b]>c)
{
tance[a][b]=tance[b][a]=c;
}
}
sc();
printf("%d\n",dis[n]);
}
}

  

poj2387 初涉最短路的更多相关文章

  1. poj2387(最短路)

    题目连接:http://poj.org/problem?id=2387 题意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离. 分析:最短路裸题. #include ...

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

  3. POJ-2387(原始dijkstra求最短路)

    Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...

  4. 【POJ2387】Til the Cows Come Home (最短路)

    题面 Bessie is out in the field and wants to get back to the barn to get as much sleep as possible bef ...

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

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

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

    题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  7. poj2387 spfa求最短路

    //Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...

  8. HDU 2544 最短路(初涉SPFA算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t ...

  9. 最短路模板(SPFA POJ2387)

    #include <set> #include <map> #include <queue> #include <stack> #include < ...

随机推荐

  1. 【读书笔记】读《JavaScript模式》 - 函数复用模式之类式继承模式

    实现类式继承的目标是通过构造函数Child()获取来自于另外一个构造函数Parent()的属性,从而创建对象. 1.类式继承模式#1 —— 默认方式(原型指向父函数实例) function Paren ...

  2. 14、Silverlight 滤镜到 UWP 滤镜的移植(二)

    上一篇文章 大致介绍了,移植戴震军大哥的 Windows Phone7 中,对于高斯模糊滤镜的移植,主要是 int[] 数组和 byte[] 数组的互相转换.同样的思路,只要把前文的方法封装一下,就能 ...

  3. Gym 100463A Crossings 逆序对

    Crossings Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description ...

  4. C#学习笔记(三)——流程控制

    一.布尔逻辑 1.与布尔有关的2元运算符 2.布尔运算符 PS:“&”与“&&“之类的区别 (1)”&“是按位运算,也就是说是将2个数都转换成2进制,然后逐个进行与操作 ...

  5. 导入/导出Excel

    --从Excel文件中,导入数据到SQL数据库中,很简单,直接用下面的语句:/*============================================================ ...

  6. chrome扩展程序开发之在目标页面运行自己的JS

    大家都知道JS是运行在客户端的,所以,如果我们自己写一个浏览器的话,是一定可以往下载下来的网页源代码中加入js的.可惜我们没有这个能力.不过幸运的是,chrome的扩展程序可以帮我们做到这件事. 本文 ...

  7. synchronized的理解

    用法解释 synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调 ...

  8. Python实践:开篇

    一.概述 Python实践 是应用Python解决实际问题的案例集合,这些案例中的Python应用通常 功能各异.大小不一. 该系列文章是本人应用Python的实践总结,会不定期更新. 二.目录 Py ...

  9. android Intent.ACTION_SEND

    ACTION_SEND intent 可以把自己的应用添加到系统的发送(分享)列表中. <intent-filter> <action android:name="andr ...

  10. python+pygame游戏开发之使用Py2exe打包游戏

    最近在用python+pygame 开发游戏,写完以后在分享给朋友玩的时候遇到了很大的问题,只有搭建了环境才能运行python脚本. 这会吓退99%以上的人……所以把我们的游戏打包(注意是打包而不是编 ...