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

就去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. IncDec Sequence(codevs 2098)

    题目描述 Description 给定一个长度为n的数列{a1,a2...an},每次可以选择一个区间[l,r],使这个区间内的数都加一或者都减一. 问至少需要多少次操作才能使数列中的所有数都一样,并 ...

  2. ajax(ajax开发与入门)

    AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术.AJAX是一种进行页面局部异步刷新的技术,局 ...

  3. WordPress环境配置与安装

    要安装wordpress,要安装apache,php,mysql,还要进行一系列复杂的配置,较为复杂. apache安装 php5.5.6 下载链接:http://windows.php.net/do ...

  4. mysql 主主复制(双主复制)+ 配置KEEPALIVED实现热备

    binlog-do-db和replicate-do-db表示需要同步的数据库 binlog-ignore-db和replicate-ignore-db表示不需要同步的数据库 云端服务器为master配 ...

  5. MVC学习笔记---MVC导出excel(数据量大,非常耗时的,异步导出)

    要在ASP.NET MVC站点上做excel导出功能,但是要导出的excel文件比较大,有几十M,所以导出比较费时,为了不影响对界面的其它操作,我就采用异步的方式,后台开辟一个线程将excel导出到指 ...

  6. obj-m

    转自:http://blog.sina.com.cn/s/blog_693301190100sxoi.html obj-m (转帖) 目标定义是Kbuild Makefile的主要部分,也是核心部分. ...

  7. Android SQLite总结(一) (转)

    Android SQLite总结(一)  郑海波 2012-08-21 转载请声明:http://blog.csdn.net/nuptboyzhb/article/details/7891887 前言 ...

  8. android获取string.xml的值(转)

    为什么需要把应用中出现的文字单独存放在string.xml文件中呢? 一:是为了国际化,当需要国际化时,只需要再提供一个string.xml文件,把里面的汉子信息都修改为对应的语言(如,English ...

  9. FPGA学习

    (一)Verilog HDL语法 一.模块 1.定义:一个电路模块/一种逻辑功能: 2.命名规则:只能是字母,数字,"$",或者'_',且开头必须是字母或者"_" ...

  10. cocos2dx游戏开发——微信打飞机学习笔记(七)——Enemy的搭建

    一.文件创建~ Enemy.h Enemy.cpp 二.How to do? 由于我是已经完成成个游戏的功能,所以我会将游戏中enemy所需要的很多功能基本上都先考虑到了,如果大家自己在做的时候也许没 ...