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

就去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. Solr的函数查询(FunctionQuery)

    作用 通过函数查询让我们可以利用 numeric域的值或者与域相关的的某个特定的值的函数,来对文档进行评分. 如何使用 这里主要有两种方法可以使用函数查询,这两种方法都是通过solr http 接口的 ...

  2. Linus的redHat在root出现错误:-bash: addgroup: command not found

    在root下无法完成添加用户组操作,如下: [root@host8 ~]# addgroup hadoop-bash: addgroup: command not found 这是的,root下竟然不 ...

  3. wifi开发总结

    转自:http://blog.csdn.net/kakaxi1o1/article/details/35625019 Unable to open connection to supplicant o ...

  4. UML中的关联关系

    UML中的关联关系其内在意思就是has a 如图:  相对于依赖关系,关联关系在代码中有所体现.上图中的关联关系在代码中体现为       其中water 中将Climate作为其中的属性. 当然,关 ...

  5. hdu 2545(并查集求节点到根节点的距离)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2545 思路:dist[u]表示节点u到根节点的距离,然后在查找的时候更新即可,最后判断dist[u], ...

  6. Eclipse 导入 Hadoop 源码

    1.准备工作 jdk: eclipse: Maven: libprotoc :https://developers.google.com/protocol-buffers/ hadoop:http:/ ...

  7. Xamarin Visual Studio提示找不到AssemblyAttributes.cs文件

    Xamarin Visual  Studio提示找不到AssemblyAttributes.cs文件   错误信息:Could not find file ‘C:\Users\[用户名]\AppDat ...

  8. DP URAL 1244 Gentlemen

    题目传送门 /* 题意:已知丢失若干卡片后剩余的总体积,并知道原来所有卡片的各自的体积,问丢失的卡片的id DP递推:首先从丢失的卡片的总体积考虑,dp[i] 代表体积为i的方案数,从dp[0] = ...

  9. 计算1至n中数字X出现的次数

    描述 计算 1 至 n 中数字 X 出现的次数,其中 $n \ge 1,X \in [0,9]$. 解题思路 这是一道比较简单的题目,举个例子先:假设 $n=11, X=1$,那么就是求 1, 2, ...

  10. css 层叠样式表

    css选择器 派生选择器 根据其元素在其上下文关系来定义样式 <style type="text/css">body{ font-size:12px; color:re ...