AC日记——热浪 codevs 1557 (最短路模板题)
德克萨斯纯朴的民眾们这个夏天正在遭受巨大的热浪!!!他们的德克萨斯长角牛吃起来不错,可是他们并不是很擅长生產富含奶油的乳製品。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)最小的总费用。
第一行: 4个由空格隔开的整数: T, C, Ts, Te
第2到第C+1行: 第i+1行描述第i条道路。有3个由空格隔开的整数: Rs, Re和Ci
一个单独的整数表示从Ts到Te的最小总费用。数据保证至少存在一条道路。
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
7
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 (最短路模板题)的更多相关文章
- 牛客小白月赛6 I 公交线路 最短路 模板题
链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...
- 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 ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
- POJ 2387 Til the Cows Come Home --最短路模板题
Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...
- hdu1874 最短路模板题
之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] ...
- HDU 2544 最短路(模板题)
求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...
- Til the Cows Come Home (最短路模板题)
个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book[1]=1就错了..... 纠结中.... Bessie is out in the field and wa ...
随机推荐
- [WP8] ListBox的Item宽度自动填满
[WP8] ListBox的Item宽度自动填满 范例下载 范例程序代码:点此下载 问题情景 开发WP8应用程序的时候,常常会需要使用ListBox作为容器来呈现各种数据集合.但是在ListBox呈现 ...
- HTML 块元素
分为3类 1. 结构块 只能包含块级元素.它们包含结构含义,但没有语义含义,也就是,不能说明内容是什么,只能说明其组织方式. <ol> <ul> <dl> < ...
- ng-click
使用ng-clcik代码是发现其内的a标签失效: 于是测试下,发现绑定在document上的click事件在点击ng-click绑定的元素上也会失效: <div ng-click="c ...
- This application is currently offline. To enable the application, remove the app_offline.htm file from the application root directory.
IIS提示:This application is currently offline. To enable the application, remove the app_offline.htm f ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q97-Q99)
04 }Which code segment should you add at line 03?A. currentItem["ClassificationMetadata"] ...
- MacOs终端忽略大小写
使用MacOs的终端时,唯一让人感觉不爽的就是Tab补全是区分大小的,所以查了资料就把这个问题搞定了.在用户目录下创建 .inputrc 文件,内容为以下三行代码,保存后重启终端再次输入文件名Tab补 ...
- 全球最低功耗蓝牙单芯片DA14580的软件体系 -RW内核和消息处理机制
上一篇文章<蓝牙单芯片DA14580的硬件架构和低功耗>阐述了DA14580的硬件架构和低功耗的工作原理.本文文章阐述该平台的软件体系,并着重分析消息事件的处理机制. 一.DA14580S ...
- 我的android学习经历12
自动匹配输入的内容(文章最后有一个问题有兴趣的可以解答一下,谢谢大神了) 这个主要是两个控件MultiAutoCompleteTextView和AutoCompleteTextView 这两个控件和T ...
- 如何获取QQ里的截图app?
电脑系统平台:OS X EI Capitan 10.11 在以前的旧的QQ版本,QQ的截图的偏好还有一个开机自启动的选项: 现在新的版本,却没有了"开机自动运行"的选项,然而有时候 ...
- 关于在Xcode的OC工程中相对路径失败的原因
Xcode的工程生成的可执行文件不是默认在源文件同一个目录下面的,所以当可执行文件执行的时候,相对路径就不对了. 这一点用终端直接编译执行文件证明了这一点: clang -fobjc-arc -fra ...