【最短路】 poj 2387
#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的更多相关文章
- 最短路 || POJ 2387 Til the Cows Come Home
从点N到1的最短路 *记得无向图两个方向都要建边就好了…… 以及多组数据= = #include <iostream> #include <cstdio> #include & ...
- 链式前向星版DIjistra POJ 2387
链式前向星 在做图论题的时候,偶然碰到了一个数据量很大的题目,用vector的邻接表直接超时,上网查了一下发现这道题数据很大,vector可定会超的,不会指针链表的我找到了链式前向星这个好东西,接下来 ...
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- 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 ...
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- 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 ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题四 最短路练习 POJ 2387 Til the Cows Come Home
求1到N的最短路 注意有重边 跑一遍dijkstra就行 /* *********************************************** Author :Sun Yuefeng ...
- 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 ...
- 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 ...
随机推荐
- Bullet_Point_Py
1. if __name__ == "__main__" Term: 模块最高级的代码:模块中没有缩进的代码 Principal 1: 第一次导入模块会执行模块最高级的代码.不管你 ...
- yum安装memcache,mongo扩展以及python的mysql模块安装
//启动memcached/usr/local/memcached/bin/memcached -d -c 10240 -m 1024 -p 11211 -u root/usr/local/memca ...
- [ An Ac a Day ^_^ ] HihoCoder 1249 Xiongnu's Land 线性扫描
拿到了icpc北京站的参赛名额 感谢亮哥~ 虽然是地狱之战 但也要全力以赴! 题意: 有一片沙漠 n片绿洲 让你用一条线分成两部分 左≥右 而且分割线要尽量靠右 问线的位置 思路: 网上说可以二分 没 ...
- 在 iOS 应用中直接跳转到 AppStore 的方法
找到应用程序的描述链接,在itunes中找到相应的应用,(如下图),点击免费右边的下方向键,选择复制链接,就拿到了对应的app的描述链接 比如:https://itunes.apple.com/c ...
- Core Animation中的基础动画
基础动画 在开发过程中很多情况下通过基础动画就可以满足开发需求,前面例子中使用的UIView代码块进行图像放大缩小的演示动画也是基础动画(在iOS7 中UIView也对关键帧动画进行了封装),只是UI ...
- Yii2.0 多条件搜索 带分页
方法一 在控制器中 ; if($titles!=""){ $where.=" and title lik ...
- python&MongoDB
1 #!/usr/bin/python 2 #-*- coding:utf-8 3 import pymongo 4 connection=pymongo.MongoClient() 5 tdb=co ...
- CSS传统布局之display属性+float属性+position属性
这三个属性是传统网页布局中经常用到的属性. 读这篇文章之前,希望你对css布局模型已经有了一定的了解.因为本文的三个属性是和css三个布局模型紧密联系在一起的.因此,如若你并不了解,我推荐你先看一下c ...
- 将当天时间转换为unix时间戳
/** * @return * * @Title: getDate * @Description: TODO(时间戳转换为String类型的日期数据) * @param @param unixDate ...
- Xcode8.2 继续使用插件
网上参考了文章:http://www.jianshu.com/p/ab819babf2c3 使用的是:update_xcode_plugins . 但要注意的是,在Xcode 8.2下安装,并没有给我 ...