#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. JQuery:以name为条件查找

    例如:$(":input[name='keleyi']") 表示查找的是name为keleyi的表单. var a=$(t).prev(":input[name='dis ...

  2. pip 安装 lxml等 出错 解决

    x86-gnu-gcc 出错 安装如下 sudo apt-get install libffi-dev sudo apt-get install libssl-dev sudo apt-get ins ...

  3. 【LeetCode】24. Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  4. Implementing a builder: Zero and Yield

    原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-builder-part1/ 前面介绍了bind和continu ...

  5. Java线程经典面试题

    53道Java线程面试题 下面是Java线程相关的热门面试题,你可以用它来好好准备面试. 1) 什么是线程? 线程是操作系统能够进行运算调度的最小单位,它被包含在进程之中,是进程中的实际运作单位.程序 ...

  6. HUST 1352 Repetitions of Substrings(字符串)

    Repetitions of Substrings Description The “repetitions” of a string S(whose length is n) is a maximu ...

  7. 非常简洁的js图片轮播

    <div id="tupian"></div><script>var jpg =new Array();jpg[0]="url(c.j ...

  8. javascript 值传递

    在js中 简单类型是值传递 复杂类型是引用传递 简单类型:String Number Boolean undefined Null 复杂类型:Object 下面的代码演示这个 var simpleNa ...

  9. html5+css3学习笔记音频和视频

    格式 IE Firefox Opera Chrome Safari Ogg No 3.5+ 10.5+ 5.0+ No MPEG 4 9.0+ No No 5.0+ 3.0+ WebM No 4.0+ ...

  10. Mac 下office 2013制作组合表

    1.选择所有数据,插入图表→柱状图.2.选中柱状图中得某一数据,如栏目量.这个在柱状图上单击一次橙色柱子就能全部选中.3.更改图表类型,改为饼状图,再单击饼状图改为折线图.