Til the Cows Come Home (最短路模板题)
个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book【1】=1就错了.....
纠结中....
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
Sample Input
- 5 5
- 1 2 20
- 2 3 30
- 3 4 20
- 4 5 20
- 1 5 100
Sample Output
- 90
Hint
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<iomanip>
- #include<algorithm>
- using namespace std;
- #define inf 1<<29
- int t,n;
- int cow[][];
- int dis[];
- int book[];
- void dijkstra()
- {
- memset(book,,sizeof(book));
- for(int i=;i<=n;i++) dis[i]=cow[][i];
- dis[]=;
- book[]=;
- for(int i=;i<=n;i++)
- {
- int mina=inf;
- int k;
- for(int j=;j<=n;j++)
- {
- if(book[j]==&&mina>dis[j])
- {
- mina=dis[j];
- k=j;
- }
- }
- book[k]=;
- for(int j=;j<=n;j++)
- {
- if(book[j]==&&dis[j]>dis[k]+cow[k][j])
- dis[j]=dis[k]+cow[k][j];
- }
- }
- }
- int main()
- {
- while(cin>>t>>n){
- int x,y,z;
- for(int i=;i<=n;i++)
- for(int j=;j<=n;j++)
- if(i==j) cow[i][j]=;
- else cow[i][j]=inf;
- for(int i=;i<=t;i++){
- cin>>x>>y>>z;
- if(cow[x][y]>z)
- cow[x][y]=cow[y][x]=z;
- }
- dijkstra();
- cout<<dis[n]<<endl;}
- return ;
- }
Bellman-Ford算法
- #include<iostream>
- #include<cstdio>
- #include<cmath>
- #include<cstring>
- #include<iomanip>
- #include<algorithm>
- using namespace std;
- #define inf 1<<29
- int t,n;
- int u[],v[],w[];
- int dis[];
- int book[];
- int main()
- {
- while(cin>>t>>n)
- {
- for(int i=;i<=t;i++)
- cin>>u[i]>>v[i]>>w[i];
- for(int i=;i<=n;i++)
- dis[i]=inf;
- dis[]=;
- for(int i=;i<=n;i++)
- for(int j=;j<=t;j++){
- if(dis[v[j]]>dis[u[j]]+w[j])
- dis[v[j]]=dis[u[j]]+w[j];
- if(dis[u[j]]>dis[v[j]]+w[j])
- dis[u[j]]=dis[v[j]]+w[j];
- }
- cout<<dis[n]<<endl;
- }
- return ;
- }
Queue
- /*
- spfa
- Memory 256K
- Time 32MS
- */
- #include <iostream>
- #include <queue>
- using namespace std;
- #define inf 1<<29
- #define MAXM 4005
- #define MAXV 1005
- typedef struct{
- int a,b,w,next;
- }Edge;
- Edge edge[MAXM];
- int n,m,headlist[MAXV];
- void spfa(){
- int i,d[MAXV],v,vis[MAXV];
- queue <int>q;
- for(i=;i<=n;i++){
- d[i]=inf;
- vis[i]=;
- }
- d[]=;
- vis[]=;
- q.push();
- while(!q.empty()){
- v=q.front();q.pop();
- vis[v]=;
- for(i=headlist[v];i!=-;i=edge[i].next)
- if(d[v]+edge[i].w<d[edge[i].b]){
- d[edge[i].b]=d[v]+edge[i].w;
- if(!vis[edge[i].b]){
- vis[edge[i].b]=;
- q.push(edge[i].b);
- }
- }
- }
- printf("%d\n",d[n]);
- }
- int main(){
- int i,a,b,c;
- while(~scanf("%d%d",&m,&n)){
- for(i=;i<=n;i++) headlist[i]=-;
- for(i=;i<=*m;i+=){
- scanf("%d%d%d",&a,&b,&c);
- edge[i].a=a;
- edge[i].b=b;
- edge[i].w=c;
- edge[i].next=headlist[a];
- headlist[a]=i;
- edge[i+].a=b;
- edge[i+].b=a;
- edge[i+].w=c;
- edge[i+].next=headlist[b];
- headlist[b]=i+;
- }
- spfa();
- }
- return ;
- }
Til the Cows Come Home (最短路模板题)的更多相关文章
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- 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模板题)
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 ( 最短路 )
题目链接: http://poj.org/problem?id=2387 Description Bessie is out in the field and wants to get back to ...
- Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化)
Til the Cows Come Home 最短路Dijkstra+bellman(普通+优化) 贝西在田里,想在农夫约翰叫醒她早上挤奶之前回到谷仓尽可能多地睡一觉.贝西需要她的美梦,所以她想尽快回 ...
- poj1511/zoj2008 Invitation Cards(最短路模板题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Invitation Cards Time Limit: 5 Seconds ...
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- [poj2449]Remmarguts' Date(K短路模板题,A*算法)
解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...
- 牛客小白月赛6 I 公交线路 最短路 模板题
链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...
随机推荐
- ruby rails 安裝
安装之前先更换Ubuntu的源 https://www.cnblogs.com/znsongshu/p/9452067.html http://gems.ruby-china.org/ 一/安 ...
- Service Fusing
服务熔断也称服务隔离,来自于Michael Nygard 的<Release It>中的CircuitBreaker应用模式,Martin Fowler在博文CircuitBreaker中 ...
- css系列(5)css的运用(一)
从本节开始介绍css配合html可以达到的一些效果. (1)导航栏: <html> <head> <title>示例5.1</title> ...
- $git学习总结系列(2)——远程仓库
本文主要介绍git本地仓库和GitHub远程仓库之间的交互和数据传输. 注:首先需要到github.com上注册一个账号. 1. 添加本地SSH Key到GitHub 要向GitHub远程仓库推送代码 ...
- TIJ读书笔记01-操作符
TIJ读书笔记01-操作符 概述 关系操作符和逻辑操作符 位操作符 类型转换 概述 操作符 操作符接受一个或多个参数,并生成一个新值. 换句话说操作符作用于操作数,生成一个新值.有些操作符会改变操 ...
- 【Head First Servlets and JSP】笔记11:cookie
容器如何知道客户是谁?(这并不是HTTP能实现的!IP地址不能唯一的标识用户,另外,非必要不采用HTTPS 继续mark孤傲苍狼的博客,百科全书 cookie——Header——字典——键值对—— 延 ...
- 【Flask】Flask-Sqlalchemy使用笔记
### 安装:```shellpip install flask-sqlalchemy``` ### 数据库连接:1. 跟sqlalchemy一样,定义好数据库连接字符串DB_URI.2. 将这个定义 ...
- Linux挂载Windows共享目录
在windows中设置共享目录并添加权限用户 把Window系统的文件共享挂载到linux centos 目录下的方法步骤: 1.先在windows下面共享需要挂载的目录. 2.确保linux与win ...
- [Python]基于CNN的MNIST手写数字识别
目录 一.背景介绍 1.1 卷积神经网络 1.2 深度学习框架 1.3 MNIST 数据集 二.方法和原理 2.1 部署网络模型 (1)权重初始化 (2)卷积和池化 (3)搭建卷积层1 (4)搭建卷积 ...
- Go 文件操作
一.读取文件 普通版 ioutil版 bufio版 二.文件写入 普通版 ioutil版 bufio版 三.文件复制 ioCopy 1.普通版读取文件 package main import ( &q ...