#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. php日期转时间戳,指定日期转换成时间戳

    写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储.处理方便,但 是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么 ...

  2. javascript基础(一)变量

    原文http://pij.robinqu.me/ 预定义的全局变量 arguments encodeURI Infinity Array encodeURIComponent isFinite Boo ...

  3. 5. Java反射机制

    Java反射机制   问题: 在运行时,对一个JAVA类,能否知道属性和方法:能否调用它的任意方法? 答案是可以的,JAVA提供一种反射机制可以实现. 目录 什么是JAVA的反射机制 JDK中提供的R ...

  4. openwrt拦截snmp报文

    SNMP使用的协议为UDP,默认端口为161和162. 使用iptables 命令如下: iptables -A INPUT -p udp -m udp --dport 161:162 -j DROP ...

  5. poj 2299 Ultra-QuickSort 逆序对模版题

    用树状数组求逆序数 唯一的坑点就是sum要用long long存 直接贴代码了 以后忘了还能直接看 2333…… PS:和hdu3743代码是一样的,因为两个都是逆序对模版题…… #include&l ...

  6. jq选择器对象筛选

    1.选择对象 1).基本 ·#id 根据给定的ID匹配一个元素.例如:$("#id")·element 根据给定的元素名匹配所有元素.例如:$("div")·. ...

  7. 4.编写Java应用程序。首先,定义一个时钟类——Clock,它包括三个int型 成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒) 进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义 一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这 些对象调用方法show()来显示时钟的时间。

    Clock package com.hanqi.test; public class Clock { int hour,minute,second; Clock(int h,int m,int s) ...

  8. Android中的shape

    在Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的形状,shape可以绘制矩形环形以及椭圆,所以只需要用椭圆即可,在使用的时候将控件比如imageview或textview ...

  9. 学习multiprocessing(2)

    1 代码1: from multiprocessing import Pool import os, time, random def long_time_task(name): print('Run ...

  10. Python 2 中的编码

    在 Python 尤其是 Python2 中,编码问题是困扰开发者尤其初学者的一大问题.什么 Unicode/UTF-8/str ,又是 decode/encode 的,搞得人头都大了.其实不然,这有 ...