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

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

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

代码~

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. using namespace std;
  5. int tance[1005][1005];
  6. bool vis[1005];
  7. int dis[1005];
  8. int t,n;
  9. void sc()
  10. {
  11. for(int i=1;i<=n;i++)
  12. vis[i]=true;
  13. vis[1]=false;
  14. for(int i=2;i<=n;i++)
  15. {
  16. dis[i]=tance[1][i];
  17. }
  18. dis[1]=0;
  19. for(int i=1;i<=n-1;i++)
  20. {
  21. int minwhere;
  22. int min=999999;
  23. for(int k=2;k<=n;k++)
  24. {
  25. if(vis[k]==true&&dis[k]<min)
  26. {
  27. min=dis[k];
  28. minwhere=k;
  29. }
  30. }
  31. vis[minwhere]=false;
  32. for(int k=2;k<=n;k++)
  33. {
  34. if(vis[k]==true)
  35. {
  36. if(dis[k]>dis[minwhere]+tance[minwhere][k])
  37. {
  38. dis[k]=dis[minwhere]+tance[minwhere][k];
  39. }
  40. }
  41. }
  42.  
  43. }
  44. return ;
  45. }
  46. int main(){
  47. while(~scanf("%d%d",&t,&n))
  48. {
  49. memset(tance,105,sizeof(tance));
  50. int a,b,c;
  51. for(int i=1;i<=t;i++)
  52. {
  53. scanf("%d%d%d",&a,&b,&c);
  54. if(tance[a][b]>c)
  55. {
  56. tance[a][b]=tance[b][a]=c;
  57. }
  58. }
  59. sc();
  60. printf("%d\n",dis[n]);
  61. }
  62. }

  

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. Android Session

    AsyncHttpClient保持session http://blog.csdn.net/ewrfedf/article/details/21968511

  2. ****CSS各种居中方法

    水平居中的text-align:center 和 margin:0 auto   这两种方法都是用来水平居中的,前者是针对父元素进行设置而后者则是对子元素.他们起作用的首要条件是子元素必须没有被flo ...

  3. VBScript [ 译 ]

    VBScript ( Visual Basic Scripting Edition) 使用COM 来和宿主环境交互.从Microsoft Windows 98 开始的所有的微软桌面操作系统的relea ...

  4. TP-Link 无线路由器设置图文教程----怎么设置TP-Link无线路由器图解

    转自:http://www.jb51.net/softjc/39399.html 无线路由器的基础配置 在我们第一次配置无线宽带路由器时,参照说明书找到无线宽带路由器默认的IP地址是192.168.1 ...

  5. andorid开发易范的错误

    1 写DAO,调用远程API接口返回不来数据,实际这个api接口经过第三方工具测试通过的,原因是没有放到线程里.

  6. Android 颜色Color(转)

    摘自:http://blog.sina.com.cn/s/blog_6f3ff2c90100t2oa.html Android中使用4个数字来表示颜色,分别是alpha.红(red).绿(green) ...

  7. Codeforces Codeforces Round #316 (Div. 2) C. Replacement set

    C. Replacement Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/proble ...

  8. loj 1155(最大流)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26869 思路:题目还是比较水的,由于点也有容量,则必须拆点,然后跑 ...

  9. Android R文件相关问题

        今天遇到的问题,gen下没有自动生成文件,而大部分java文件中错误是找不到R.java.“R cannot be resolved to a variable” 这就一定有别的原因造成错误, ...

  10. WebView的写法

    public class MainActivity extends Activity { private WebView wv; @Override protected void onCreate(B ...