#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. notepad 是doc 调出记事本文件

    notepad 是doc 调出记事本文件notepad 是doc 调出记事本文件notepad 是doc 调出记事本文件

  2. navicat 连接远程mysql

    01putty中session---远程地址(程序访问的域名) ,端口22--load加载进来 02SSH--Auth-Tunnels(隧道)-- putty端口映射SSH--Auth-Tunnels ...

  3. Java 4

    1.继承的问题 子类是父类的一个扩展,子类可以利用父类的属性与行为,这种情况子类会破坏父类的封装 为了保持父类良好的封装性,设计父类有以下规则: 如果要把某类设计为最终类则需要添加final修饰符,或 ...

  4. LeetCode OJ 31. Next Permutation

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. Visual Studio 2010/2013 UTF8编码调试时显示中文

    VisualStudio 2010 SP1环境 1.设置string默认编码为utf8,只需要在文件头部加入以下代码 #pragma execution_character_set("utf ...

  6. NOIP2010-普及组复赛模拟试题-第二题-数字积木

    题目描述 Description 小明有一款新式积木,每个积木上都有一个数,一天小明突发奇想,要是把所有的积木排成一排,所形成的数目最大是多少呢?你的任务就是读入 n 个数字积木,求出所能形成的最大数 ...

  7. 网络通信框架Apache MINA

    Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...

  8. 关于css的hack问题

    <!--[if <keywords>? IE <version>?]> HTML代码块 <![endif]--> 取值: <keywords> ...

  9. angularJS 系列(七)---指令

    ----------------------------------------------------------------------------------- 原文:https://www.s ...

  10. php 生成json格式的数据

    放到引入的公共函数里边 if (!function_exists('format_json')) { /** * 格式化API输出的json * @param $return_code string ...