题目

#define  _CRT_SECURE_NO_WARNINGS

#include<string.h>
#include<stdio.h>
#include<math.h> const int MAXN=; #define typec int
const typec INF=0x3f3f3f3f;//防止后面溢出,这个不能太大
bool vis[MAXN];
int pre[MAXN];
typec cost[MAXN][MAXN];
typec lowcost[MAXN];
void Dijkstra(int n,int beg)
{
for(int i=;i<=n;i++)
{
lowcost[i]=INF;vis[i]=false;pre[i]=-;
}
lowcost[beg]=;
for(int j=;j<=n;j++)
{
int k=-;
int Min=INF;
for(int i=;i<=n;i++)
if(!vis[i]&&lowcost[i]<Min)
{
Min=lowcost[i];
k=i;
}
if(k==-)break;
vis[k]=true;
for(int i=;i<=n;i++)
if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i])
{
lowcost[i]=lowcost[k]+cost[k][i];
pre[i]=k;
}
}
} int main()
{
int n,i,j,t,a,b,c;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(i=;i<=n;i++)
for(j=;j<=n;j++)
cost[i][j]=(i==j)? :INF; for(i=;i<t;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(cost[a][b]>c)
cost[a][b]=cost[b][a]=c;
}
Dijkstra(n,n);
printf("%d\n",lowcost[]);
}
return ;
}

poj 2387 Til the Cows Come Home (最短路,dijkstra模版题)的更多相关文章

  1. POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)

    题目连接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...

  2. POJ 2387 Til the Cows Come Home(最短路模板)

    题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...

  3. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  4. 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 ...

  5. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  6. Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)

    Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...

  7. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  8. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  9. 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33015   Accepted ...

  10. 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 ...

随机推荐

  1. Struts2+Hibernate4+Spring4整合

    jar包 配置文件 web.xml文件 <!-- needed for ContextLoaderListener --> <context-param> <param- ...

  2. sql常用的星期方法

    sql常用的星期方法: SELECT convert(varchar(10),DATEADD(wk, DATEDIFF(wk,0,getdate()), 0),120) --本周开始周一SELECT ...

  3. 常用JS加密编码算法

    //#region UTF8编码函数 function URLEncode(Str) { if (Str == null || Str == "") return "&q ...

  4. jquery 在页面中三种写法

    jQuery 分 2 个系列版本 1.x 与 2.x,主要的区别在于 2.x 不再兼容 IE6.7.8浏览器,这样做的目的是为了兼容移动端开发.由于减少了一些代码,使得该版本比 jQuery 1.x ...

  5. WCF完全解析读书笔记第2章地址

    1. 使用同一个绑定对象实现地址跨终结点共享 2. 地址报头帮助辅助寻址 3. 使用端口共享为多个服务使用相同端口 4. WCF终结点地址分为逻辑地址和物理地址, 客户端使用ClientViaBeha ...

  6. How to check Windows 7 OS is permanently activated?[Windows 7]

    Press Windows + R, then you can enter : slmgr.vbs -xpr

  7. Python-Day5 常用模块学习

    一.模块介绍 通俗点说,就是把常用的一些功能单独放置到一个.py文件中,方便其他文件来调用,这样的一个文件可以称为一个模块. 模块分为三种: 自定义模块 内置标准模块(又称标准库) 开源模块 二.导入 ...

  8. SQLdiag Utility

    使用SQLdiag 会进行信息搜集类型 Windows 系统性能日志 Windows 系统日志 SQL Server 性能信息 SQL Server 阻塞信息 SQL Server 配置信息 如何使用 ...

  9. ASP.NET从数据库中取出数据,有数据的复选框为选中

    在KS系统中在更新菜单的时候,当查出菜单的时候要查出菜单下面已经有了哪些界面了我用了一下的方法弄的.代码如下: 界面代码: <%@ Page Language="C#" Au ...

  10. SSHDroid(SSH Server for Android)通过PC或命令连接android

    1.下载berserker.android.apps.sshdroid.apk .(如果你懒的下载,给我留言,我会发给你) 2.安装到手机,显示如图: 简单解释一下:一般android系统没有root ...