#include <iostream>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
using namespace std;
int map[][];
int dis[];
int n,pos,sum;
void init()
{
for(int i=; i<; i++)
{
for(int k=; k<; k++)
map[i][k] = INT_MAX;
map[i][i] = ;
}
for(int i=; i<; i++)
dis[i] = INT_MAX;
sum = ;
}
void Dijkstra()
{
int used[];
memset(used,,sizeof(used));
int now = pos;
dis[now] = ; used[now] = ;
for(int i=; i<n; i++)
{
for(int k=; k<=n; k++)
if( map[now][k] != INT_MAX && dis[now] + map[now][k] < dis[k] )
dis[k] = dis[now] + map[now][k];
int min = INT_MAX;
for(int k=; k<=n; k++)
if( dis[k] < min && !used[k] )
min = dis[now = k];
used[now] = ;
}
}
int main()
{
int from,to,len,t;
init();
cin >> t >> n;
pos = n;
for(int i=; i<=t; i++)
{
cin >> from >> to >> len;
if( len < map[from][to] )
map[from][to] = map[to][from] = len;
}
Dijkstra();
cout << dis[] << endl;
return ;
} #include<iostream>
using namespace std;
#define MAX 1005
const int oo=;
int dist[MAX][MAX];
int close[MAX];
bool used[MAX];
int main()
{
int N,T,i,j;
int s,e,len;
scanf("%d%d",&T,&N);
memset(dist,0x7f,sizeof(dist));
memset(used,false,sizeof(used));
memset(close,0x7f,sizeof(close));
for(i=;i<=T;i++)
{
scanf("%d%d%d",&s,&e,&len);
if(dist[s][e]>len)
dist[s][e]=dist[e][s]=len;
}
for(i=;i<=N;i++)
{
close[i]=dist[][i];
}
used[]=true;
for(i=;i<=N;i++)
{
int min=;int mlen=oo;
for(j=;j<=N;j++)
{
if(!used[j]&&close[j]<mlen)
{
min=j,mlen=close[j];
}
}
used[min]=true;
for(j=;j<=N;j++)
{
if(!used[j]&&dist[min][j]<oo)
{
int temp=dist[min][j]+close[min];
if(temp<close[j])
close[j]=temp;
}
}
}
printf("%d\n",close[N]);
return ;
}

【最短路】 poj 2387的更多相关文章

  1. 最短路 || POJ 2387 Til the Cows Come Home

    从点N到1的最短路 *记得无向图两个方向都要建边就好了…… 以及多组数据= = #include <iostream> #include <cstdio> #include & ...

  2. 链式前向星版DIjistra POJ 2387

    链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...

  3. hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题

    hdu 2544  求点1到点n的最短路  无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...

  4. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  5. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

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

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  7. POJ 2387 Til the Cows Come Home (最短路 dijkstra)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...

  8. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home

    求1到N的最短路 注意有重边 跑一遍dijkstra就行 /* *********************************************** Author :Sun Yuefeng ...

  9. POJ 2387 Til the Cows Come Home 【最短路SPFA】

    Til the Cows Come Home Description Bessie is out in the field and wants to get back to the barn to g ...

  10. POJ - 2387 Til the Cows Come Home (最短路Dijkstra+优先队列)

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

随机推荐

  1. Bullet_Point_Py

    1. if __name__ == "__main__" Term: 模块最高级的代码:模块中没有缩进的代码 Principal 1: 第一次导入模块会执行模块最高级的代码.不管你 ...

  2. yum安装memcache,mongo扩展以及python的mysql模块安装

    //启动memcached/usr/local/memcached/bin/memcached -d -c 10240 -m 1024 -p 11211 -u root/usr/local/memca ...

  3. [ An Ac a Day ^_^ ] HihoCoder 1249 Xiongnu's Land 线性扫描

    拿到了icpc北京站的参赛名额 感谢亮哥~ 虽然是地狱之战 但也要全力以赴! 题意: 有一片沙漠 n片绿洲 让你用一条线分成两部分 左≥右 而且分割线要尽量靠右 问线的位置 思路: 网上说可以二分 没 ...

  4. 在 iOS 应用中直接跳转到 AppStore 的方法

      找到应用程序的描述链接,在itunes中找到相应的应用,(如下图),点击免费右边的下方向键,选择复制链接,就拿到了对应的app的描述链接 比如:https://itunes.apple.com/c ...

  5. Core Animation中的基础动画

    基础动画 在开发过程中很多情况下通过基础动画就可以满足开发需求,前面例子中使用的UIView代码块进行图像放大缩小的演示动画也是基础动画(在iOS7 中UIView也对关键帧动画进行了封装),只是UI ...

  6. Yii2.0 多条件搜索 带分页

                                   方法一   在控制器中 ; if($titles!=""){ $where.=" and title lik ...

  7. python&MongoDB

    1 #!/usr/bin/python 2 #-*- coding:utf-8 3 import pymongo 4 connection=pymongo.MongoClient() 5 tdb=co ...

  8. CSS传统布局之display属性+float属性+position属性

    这三个属性是传统网页布局中经常用到的属性. 读这篇文章之前,希望你对css布局模型已经有了一定的了解.因为本文的三个属性是和css三个布局模型紧密联系在一起的.因此,如若你并不了解,我推荐你先看一下c ...

  9. 将当天时间转换为unix时间戳

    /** * @return * * @Title: getDate * @Description: TODO(时间戳转换为String类型的日期数据) * @param @param unixDate ...

  10. Xcode8.2 继续使用插件

    网上参考了文章:http://www.jianshu.com/p/ab819babf2c3 使用的是:update_xcode_plugins . 但要注意的是,在Xcode 8.2下安装,并没有给我 ...