POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法。
关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的。
写法1:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
using namespace std;
#define N 1007 int mp[N][N],n,m;
int dis[N],vis[N]; void Dijastra(int s)
{
int now = s;
int i,k;
dis[now] = ;
vis[now] = ;
for(i=;i<=n;i++)
{
for(k=;k<=n;k++) //order 1
{
if(mp[now][k] != Mod && dis[now] + mp[now][k] < dis[k])
dis[k] = dis[now] + mp[now][k];
}
int mini = Mod; //order 2
for(k=;k<=n;k++)
{
if(dis[k] < mini && !vis[k])
{
now = k;
mini = dis[k];
}
}
vis[now] = ;
}
} int main()
{
int u,v,w,i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=;i<=n;i++)
dis[i] = Mod;
dis[] = ;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
mp[i][j] = Mod;
mp[i][i] = ;
}
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(w < mp[u][v])
mp[u][v] = mp[v][u] = w;
}
memset(vis,,sizeof(vis));
Dijastra();
printf("%d\n",dis[n]);
}
return ;
}
写法2:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
using namespace std;
#define N 1007 int mp[N][N],n,m;
int dis[N],vis[N]; void Dijastra(int s)
{
int now;
int i,k;
dis[s] = ;
for(i=;i<=n;i++)
{
int mini = Mod; //order 1
for(k=;k<=n;k++)
{
if(dis[k] < mini && !vis[k])
{
now = k;
mini = dis[k];
}
}
vis[now] = ;
for(k=;k<=n;k++) //order 2
{
if(mp[now][k] != Mod && dis[now] + mp[now][k] < dis[k])
dis[k] = dis[now] + mp[now][k];
}
}
} int main()
{
int u,v,w,i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=;i<=n;i++)
dis[i] = Mod;
dis[] = ;
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
mp[i][j] = Mod;
mp[i][i] = ;
}
while(m--)
{
scanf("%d%d%d",&u,&v,&w);
if(w < mp[u][v])
mp[u][v] = mp[v][u] = w;
}
memset(vis,,sizeof(vis));
Dijastra();
printf("%d\n",dis[n]);
}
return ;
}
POJ 2387 Til the Cows Come Home --最短路模板题的更多相关文章
- POJ 2387 Til the Cows Come Home(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home (dijkstra模板题)
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
- POJ 2387 Til the Cows Come Home (最短路径 模版题 三种解法)
原题链接:Til the Cows Come Home 题目大意:有 个点,给出从 点到 点的距离并且 和 是互相可以抵达的,问从 到 的最短距离. 题目分析:这是一道典型的最短路径模版 ...
- POJ 2387 Til the Cows Come Home(dijkstra裸题)
题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...
- Til the Cows Come Home(最短路模板题)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description Bessie is ...
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- (简单) POJ 2387 Til the Cows Come Home,Dijkstra。
Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...
随机推荐
- 动态创建JS
var element=document.createElement('script'); element.setAttribute('src', './js/move.js'); document. ...
- JavaScripts 基础详细笔记整理
一.JS简介 JavaScript 是 Web 的编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理 ...
- SharpGL学习笔记(十七) 立体文字和平面文字
在写有关文字的主题前,笔者翻阅了几本书上的相关章节,研究了几天无果. 徐明亮<OpenGL游戏编程>书中介绍的是“位图字体”,也就是把字体栅格化,然后画出来.照着书上的VC代码翻译为C#的 ...
- SQL数据库基础(九)
变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...
- Play 内置模板标签(1.2.3版本)http://www.anool.net/?p=617
a标签: 用来插入一个连接到控制器方法的html link.如下: #{a @Application.logout()}Disconnect#{/a}模板内容被解析后变成: <a href=&q ...
- 开启Windows Server 2008 R2上帝模式
TAG标签: 摘要:这个“God Mode” 应该大部分的网友都听过了,只是在 Windows Server 2008 R2 上也支持此一功能.启用方式非常简单,在桌面新建一个文件夹,命名为: God ...
- ASP.NET MVC another entity of the same type already has the same primary key value
ASP.NET MVC项目 Repository层中,Update.Delete总是失败 another entity of the same type already has the same pr ...
- Android Studio利用Gradle删除没有使用到的资源和代码文件
一.打包时忽略无用资源 我们在打包的时候默认会把没有用到的资源(比如图片)也打包成app,徒增了应用的大小.现在我们可以利用Gradle来优雅的去除没有用到的资源文件了! 就是在gradle中配置sh ...
- 上传Android代码到Jcenter(解决了字符映射的问题)
请先阅读:http://blog.saymagic.cn/2015/02/16/release-library-to-jcenter.html 最外面的build.gradle // Top-leve ...
- Android WebView 获取网页的标题
final TextView txtTitle = (TextView) findViewById(R.id.txtTitle); final WebView webView = (WebView)f ...