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 ...
随机推荐
- ahjesus SSHkey登陆linux服务器,无需密码,ubuntu
cd ~/.ssh/如果目录不存在就新建一个 mkdir ~/.ssh 制作公匙 ssh-keygen -t rsa默认会生成id_rsa.pub的公匙 将公匙推送到指定的服务器 scp id_rsa ...
- nginx跨域处理
http://www.nginx.cn/nginx-download nginx.conf配置 if ($request_method = ‘OPTIONS’) { add_heade ...
- hdu 1518 拼正方形
本题来自:http://acm.hdu.edu.cn/showproblem.php?pid=1518 题意:输入几个长度,判断能否拼成正方形. 以下部分参考了网友代码,终于ac啦. #include ...
- 备份一张iPhone拍照写入exif中的orientation图片
- webservice 的wsdl文件生成客户端java类
提供两个方法: 第一个: 发布webservice项目后, 地址栏地址 http://localhost:8888/lxitedu.webservice.cxf-ch2/services/userS ...
- ArcMap中的名称冲突问题
这是一个非常有趣的问题. 现在我一个点层叫"汶川73",有一个面层也叫"汶川73",使用空间连接工具来计算每个面中被落入有多少个点.但不管怎么算,结果都只能生成 ...
- CRM 2013 移动终端 介绍和PAD下载地址
IPHONE 浏览器界同 Pad 端 APP (目前不支持中文,大家可以用美国账号下载,谁有分享一下) https://itunes.apple.com/en/app/microsoft-dynam ...
- Arcgis for android 离线查询
参考.. 官方API demo ... 各种资料 以及.. ArcGIS for Android示例解析之高亮要素-----HighlightFeatures ttp://blog.csdn.net/ ...
- sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户
现象: sharepoint获取exchange邮箱报错:该帐户无权模拟所请求的用户 处理办法: 1.Open the Exchange Management Shell 2.输入: New-Mana ...
- nutch简介
1.什么是 nutch Nutch 是一个开源的. Java 实现的搜索引擎.它提供了我们运行自己的搜 索引擎所需的全部工具.2.研究 nutch 的原因(1) 透明度: nutch 是开放源代码的, ...