1557 热浪

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 钻石 Diamond
 查看运行结果
 
 
题目描述 Description

德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。Farmer John此时以先天下之忧而忧,后天下之乐而乐的精神,身先士卒地承担起向德克萨斯运送大量的营养冰凉的牛奶的重任,以减轻德克萨斯人忍受酷暑的痛苦。

FJ已经研究过可以把牛奶从威斯康星运送到德克萨斯州的路线。这些路线包括起始点和终点先一共经过T (1 <= T <= 2,500)个城镇,方便地标号為1到T。除了起点和终点外地每个城镇由两条双向道路连向至少两个其它地城镇。每条道路有一个通过费用(包括油费,过路费等等)。

给定一个地图,包含C (1 <= C <= 6,200)条直接连接2个城镇的道路。每条道路由道路的起点Rs,终点Re (1 <= Rs <= T; 1 <= Re <= T),和花费(1 <= Ci <= 1,000)组成。求从起始的城镇Ts (1 <= Ts <= T)到终点的城镇Te(1 <= Te <= T)最小的总费用。

输入描述 Input Description

第一行: 4个由空格隔开的整数: T, C, Ts, Te

第2到第C+1行: 第i+1行描述第i条道路。有3个由空格隔开的整数: Rs, Re和Ci

输出描述 Output Description

一个单独的整数表示从Ts到Te的最小总费用。数据保证至少存在一条道路。

样例输入 Sample Input

7 11 5 4

2 4 2

1 4 3

7 2 2

3 4 3

5 7 5

7 3 3

6 1 1

6 3 4

2 4 3

5 6 3

7 2 1

样例输出 Sample Output

7

数据范围及提示 Data Size & Hint

5->6->1->4 (3 + 1 + 3)

思路:

  裸spfa;

来,上代码:

#include<queue>
#include<cstdio>
#include<iostream> using namespace std; struct node {
int to,dis,next;
};
struct node edge[]; int num_head,num_edge,num,start,end,head[],dis[]; char word; void spfa(int all_from)
{
queue<int>que;
bool if_in_spfa[];
for(int i= ; i<=num_head ; i++) dis[i]=,if_in_spfa[i]=false;
dis[all_from]=;
if_in_spfa[all_from]=true;
que.push(all_from);
int cur_1;
while(!que.empty())
{
cur_1=que.front();
que.pop();
for(int i=head[cur_1] ; i ; i=edge[i].next)
{
if(dis[cur_1]+edge[i].dis<dis[edge[i].to])
{
dis[edge[i].to]=dis[cur_1]+edge[i].dis;
if(!if_in_spfa[edge[i].to])
{
if_in_spfa[edge[i].to]=true;
que.push(edge[i].to);
}
}
}
if_in_spfa[cur_1]=false;
}
} inline void read_int(int &now_001)
{
now_001=;word=getchar();
while(word<''||word>'') word=getchar();
while(word<=''&&word>='')
{
now_001=now_001*+(int)(word-'');
word=getchar();
}
} inline void edge_add(int from,int to,int dis)
{
num++;
edge[num].to=to;
edge[num].dis=dis;
edge[num].next=head[from];
head[from]=num;
} int main()
{
ios::sync_with_stdio(false);
read_int(num_head),read_int(num_edge),read_int(start),read_int(end);
int from,to,dis1;
for(int i= ; i<=num_edge ; i++)
{
read_int(from),read_int(to),read_int(dis1);
edge_add(from,to,dis1);
edge_add(to,from,dis1);
}
spfa(start);
printf("%d\n",dis[end]);
return ;
}

AC日记——热浪 codevs 1557 (最短路模板题)的更多相关文章

  1. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

  2. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  3. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  4. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  5. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

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

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

  7. hdu1874 最短路模板题

    之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] ...

  8. HDU 2544 最短路(模板题)

    求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...

  9. Til the Cows Come Home (最短路模板题)

    个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book[1]=1就错了..... 纠结中.... Bessie is out in the field and wa ...

随机推荐

  1. Ahjesus Nodejs02 使用集成开发环境

    下载最新版webstorm, 选择此集成开发环境是因为支持性较好,在vs下也有插件支持,不过感觉有些牵强 附vs插件 NTVS 详细介绍 安装好以后就需要配置npm NPM 国内高速镜像 source ...

  2. linux怎么模糊查找一个文件

    linux如何模糊查找一个文件 在当前目录下搜索指定文件: find . -name test.txt 在当前目录下模糊搜索文件: find . -name '*.txt' 在当前目录下搜索特定属性的 ...

  3. html button自动提交表单问题

    在ie中,button默认的type是button,而其他浏览器和W3C标准中button默认的属性都是submit,所以在chrome中,需要使用<button type="butt ...

  4. java File.mkdirs和mkdir区别

    File f = new File("e://xxx//yyy"); System.out.println(f.mkdirs());//生成所有目录,一般来说,这个方法稳健性更好, ...

  5. C# Winform 窗体美化

    using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using Sys ...

  6. CentOS下apache绑定域名

    本文主要介绍在CentOS下apache绑定域名以及apache绑定多个域名,首先要找到apache的配置文件httpd.conf的位置.CentOS操作系统一般在 /etc/httpd/conf 下 ...

  7. APP落地页开发中的一些小经验~

    在开发日常落地页的时候,每当碰到一些很酷炫的宣传图用css实现很复杂且耗时的时候,一般采取切图然后将其放在页面中,在这个过程中发现<img/>标签中图片下方会有一行小空白,影响了与后一部分 ...

  8. How to Build Office Developer Tools Projects with TFS Team Build 2012

    Introduction Microsoft Visual Studio 2012 provides a new set of tools for developing apps for Office ...

  9. 【转】Android Studio中通过快捷键来提取提取方法

    今天来给大家介绍一个非常有用的Studio Tips,有些时候我们在一个方法内部写了过多的代码,然后想要把一些代码提取出来再放在一个单独的方法里,通常我们的做法是复制粘贴,现在我来教给大家一个非常简洁 ...

  10. UISegmentedControl的使用

    #import "SegmentedControlTestViewController.h" @implementation SegmentedControlTestViewCon ...