Til the Cows Come Home(最短路)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Description
Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
Output
#include <iostream>
#include <string>
#include <queue>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <deque>
#include <vector>
#define LL long long
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define dg(i) cout << "*" << i << endl;
using namespace std; /**
*spfa算法:邻接矩阵+SLF策略
*顶点标号:1..n
*源点:1,目的点:n
*/ const int sz = ; //size--最大顶点数
const int inf = 0x3f3f3f3f; //最大值
int w[sz][sz]; //w[i][j]--边(i,j)的权值,若(i,j)不存在,w[i][j]为inf
int dis[sz]; //dis[i]--源点到顶点i的最短距离,初始化为inf
bool in[sz]; //in[i]--标记顶点i是否在队列中,在为true
int n, m; //n--顶点数,m--边数 int main()
{
while(scanf("%d %d", &m, &n) != EOF)
{
int u, v, d;
memset(dis, inf, sizeof(dis)); //0x3f3f3f3f是可以用memset初始化的
dis[] = ; //
memset(w, inf, sizeof(w));
while(m--)
{
scanf("%d %d %d", &u, &v, &d);
w[u][v] = w[v][u] = min(d, w[u][v]); //为避免平行边,取min
}
memset(in, false, sizeof(in));
deque<int> que;
que.push_front(); //源点进队
in[] = true;
while(!que.empty())
{
int cur = que.front();
que.pop_front();
in[cur] = false; //撤销进队标志
for(int i = ; i <= n; i++) //对与cur相邻的点都进行松弛计算
{
if(dis[cur] + w[cur][i] < dis[i])
{
dis[i] = dis[cur] + w[cur][i]; //更新最短距离
if(!in[i]) //对于最短距离被更新的点,若不在队列则进队
{
//此处执行SLF策略,小的尽量放前面。不采取这个策略而直接让i进队也可以
if(!que.empty() && dis[i] < dis[que.front()])
que.push_front(i);
else que.push_back(i);
in[i] = true;
}
}
}
}
printf("%d\n", dis[n]);
}
return ;
}
Til the Cows Come Home(最短路)的更多相关文章
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- 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 Til the Cows Come Home ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- 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(最短路模板)
题目链接:http://poj.org/problem?id=2387 题意:有n个城市点,m条边,求n到1的最短路径.n<=1000; m<=2000 就是一个标准的最短路模板. #in ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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 ...
- 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 ...
- 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(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
随机推荐
- Null 与 “” 的区别
说明:很多人有时候对于 null 和 "" 不是很清楚,结合其他人的文章,今天做下解释. String str1 = null; str引用为空 String str2 = &qu ...
- [Python] 中文编码问题:raw_input输入、文件读取、变量比较等str、unicode、utf-8转换问题
最近研究搜索引擎.知识图谱和Python爬虫比较多,中文乱码问题再次浮现于眼前.虽然市面上讲述中文编码问题的文章数不胜数,同时以前我也讲述过PHP处理数据库服务器中文乱码问题,但是此处还是准备简单做下 ...
- paip.语义分析--单字词形容词表180个
paip.语义分析--单字词形容词表180个 INSERT INTO t (word) SELECT DISTINCT word FROM `word_main` where tsisin is ...
- jQuery自动分页打印表格(HTMLtable),可以强制换页
最近做项目的时候需要做批量打印订单,一个订单可能在最后一页是的内容是不足一页的,这时候下一个订单需要下一页打印,这样就需要强制换页.在下一页再打印下一个订单 部分代码: 部分重要的css是分页的换页的 ...
- GEF - 制作一个简单图形化编辑框架笔记1
在首先来看看GEF是什么,GEF的全称是Graphical Editing Framework,图形化框架,可以利用此框架做图形化编.他的基本原理是采用MVC开发模式. 以下是一些例子 GEF里面包含 ...
- React Ajax
React 组件的数据可以通过 componentDidMount 方法中的 Ajax 来获取, 当从服务端获取数据库可以将数据存储在 state 中,再用 this.setState 方法重新渲染 ...
- Reorder List
题目: Given a singly linked list L: L0→L1→-→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do ...
- SQL Server 内存中OLTP内部机制概述(四)
----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...
- 多台linux服务器时间同步
1,设置A机时间服务器: a,修改 /etc/ntp.conf,如下: # Undisciplined Local Clock. This is a fake driver intended for ...
- APK重签名总结
keytool -genkey -alias aeo_android.keystore -keyalg RSA -validity 20000 -keystore aeo_android.keysto ...