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

就去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. 扩展HtmlHelper方法

    1.在Model中新建类MyHtmlHelperExt /// <summary> /// 扩展HtmlHelper方法 /// 扩展方法三要素:静态类,静态方法,this关键字 /// ...

  2. GCM 发送接收消息 Message Client Server 服务器端,客户端

    GCM 传递参数 最近用了很多时间做GCM,由于碰到很多问题,因此详细做一下记录,以方便各位网友,不用再走我的重复的路.不过我试了一下GCM在国内很不好用.假如开发国外的程序的话,用GCM倒是很不错的 ...

  3. svn 创建

    1.ps aux | grep svn 杀掉进程 2.svnadmin create /svnrepertory/SVNwangping 创建svn仓库; 3.修改3个文件 4.svnserve -d ...

  4. Java 四舍五入

    http://www.cnblogs.com/xd502djj/archive/2011/07/21/2112683.html

  5. hdu 1465:不容易系列之一(递推入门题)

    不容易系列之一 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  6. Java Hour 13 集合基础

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 本文作者Java 现经验约为13 Hour,请各位不吝赐教. Java 中的集 ...

  7. 分享一款超棒的jQuery旋钮插件 - jQuery knob

    转自:http://www.cnblogs.com/gbin1/archive/2012/05/08/2489908.html 在线演示  本地下载 如果你也在寻找一款生成漂亮旋钮(knob)的jQu ...

  8. C/C++中extern关键字详解

    转自:http://www.cnblogs.com/yc_sunniwell/archive/2010/07/14/1777431.html 1 基本解释:extern可以置于变量或者函数前,以标示变 ...

  9. java web开发问题集合

    前台和后台的交流到底是借助什么?servlet?xml? 我们能感觉到用servlet,但是我们是如何使用servlet的?不是现在web.xml部署后,才能触发吗?所以其实我们是本质是借助XML文件 ...

  10. 【codevs1993】草地排水 最大流

    [codevs1993]草地排水 题目描述 Description 在农夫约翰的农场上,每逢下雨,Bessie最喜欢的三叶草地就积聚了一潭水.这意味着草地被水淹没了,并且小草要继续生长还要花相当长一段 ...