poj2387 初涉最短路
前两天自学了一点点最短路..看起来很简单的样子...
就去kuangbin的专题找了最简单的一道题练手..然后被自己萌萌的三重for循环超时虐的不要不要的~
松弛虽然会但是用的十分之不熟练...
代码~
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int tance[1005][1005];
bool vis[1005];
int dis[1005];
int t,n;
void sc()
{
for(int i=1;i<=n;i++)
vis[i]=true;
vis[1]=false;
for(int i=2;i<=n;i++)
{
dis[i]=tance[1][i];
}
dis[1]=0;
for(int i=1;i<=n-1;i++)
{
int minwhere;
int min=999999;
for(int k=2;k<=n;k++)
{
if(vis[k]==true&&dis[k]<min)
{
min=dis[k];
minwhere=k;
}
}
vis[minwhere]=false;
for(int k=2;k<=n;k++)
{
if(vis[k]==true)
{
if(dis[k]>dis[minwhere]+tance[minwhere][k])
{
dis[k]=dis[minwhere]+tance[minwhere][k];
}
}
} }
return ;
}
int main(){
while(~scanf("%d%d",&t,&n))
{
memset(tance,105,sizeof(tance));
int a,b,c;
for(int i=1;i<=t;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(tance[a][b]>c)
{
tance[a][b]=tance[b][a]=c;
}
}
sc();
printf("%d\n",dis[n]);
}
}
poj2387 初涉最短路的更多相关文章
- poj2387(最短路)
题目连接:http://poj.org/problem?id=2387 题意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离. 分析:最短路裸题. #include ...
- POJ2387 Til the Cows Come Home (最短路 dijkstra)
AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...
- POJ-2387(原始dijkstra求最短路)
Til the Cows Come Home POJ-2387 这题是最简单的最短路求解题,主要就是使用dijkstra算法,时间复杂度是\(O(n^2)\). 需要注意的是,一定要看清楚题目的输入要 ...
- 【POJ2387】Til the Cows Come Home (最短路)
题面 Bessie is out in the field and wants to get back to the barn to get as much sleep as possible bef ...
- POj2387——Til the Cows Come Home——————【最短路】
A - Til the Cows Come Home Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & ...
- POJ-2387 Til the Cows Come Home ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- poj2387 spfa求最短路
//Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...
- HDU 2544 最短路(初涉SPFA算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t ...
- 最短路模板(SPFA POJ2387)
#include <set> #include <map> #include <queue> #include <stack> #include < ...
随机推荐
- google登录不了解决喽
大家好,google 每到这个时候就登录不聊了.... 解法: 修改host 文件 下载地址点我
- 元素查找(codevs 1230)
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给出n个正整数,然后有m个询问,每 ...
- 在Android4.4上新增加keycode
keycode是android定义好的,但是有时候无法满足需要,进行定制化难免会涉及到新增加keycode.分成两部分,驱动和framework,这里主要讲解framework部分: 一.驱动部分: ...
- wp8 入门到精通 ---时间
DateTime.Now.ToShortTimeString()DateTime dt = DateTime.Now;dt.ToString();//2005-11-5 13:21:25dt.ToFi ...
- Adapter适配器
1.概念 *连接后端数据和前端显示的适配器接口 *数据和UI之间的重要连接 2. ArrayAdapter ArrayAdapter构造器如下: ArrayAdapter(Context con ...
- asp.net控件开发基础(1)(转)原文更多内容
asp.net本身提供了很多控件,提供给我们这些比较懒惰的人使用,我认为控件的作用就在此,因为我们不想重复工作,所以要创建它,这个本身便是一个需求的关系,所以学习控件开发很有意思. wrox网站上有本 ...
- 2016.6.11 ASP提交数据到SQL server数据乱码解决方法
1.检查数据库排序规则 China-PRE-90-CS-AI 2.ASP文档中,写入数据的页面的编码和检查提交数据页面的编码一致:
- js:数据结构笔记1---数组
JS中数组: 只是一种特殊的对象,比其他语言中效率低: 属性是用来表示偏移量的索引:在JS中,数字索引在内部被转化为字符串类型(这也是为什么写对象属性的时候可以不叫引号),因为对象中的属性必须是字符串 ...
- 手持PDA智能条码扫描RFID打印POS机
手持PDA智能条码扫描RFID打印POS机 一.系统稳定性: 1.硬件稳定性: 采用了华为海思(国内唯一可以媲美全球顶级的CPU+射频方案厂商,可以和英伟达等一决高下)手机方案,CPU+射频浑然一 ...
- windows 并发与同步 学习笔记
测试 5.2 windows 中断与异常 1.外部硬件中断是通过处理器上的中断引脚管或者本地APIC的内置模块来发生的:对于一个处理器,一旦被中单, 某个预先被设定的中断服务例程就被执行! 2.处理器 ...