最短路<dijk>
题意:
有n个城市,有m条路,给出每条路的出发和结束的城市及长度,求从第一个城市到最后一个城市的最短路。按格式输出。
题解:
标准dijk算法。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=505;
typedef long long LL;
int W[maxn][maxn];
int vis[maxn];
int dis[maxn];
int n,m;
void init()
{
for(int i=0;i<maxn;i++)
{
for(int j=0;j<maxn;j++)
W[i][j]=INF;
W[i][i]=0;
}
memset(vis,0,sizeof(vis));
}
void dijk()
{
for(int i=0;i<n;i++) dis[i]=(i==0?0:INF);
for(int i = 0;i < n;i++)
{
int m = INF , x=0;
for(int y = 0;y <n;y++) if(!vis[y] && dis[y] <= m) m = dis[x=y];
vis[x] = true;
for(int y = 0;y < n;y++) dis[y]=min(dis[y],dis[x]+W[x][y]);
}
}
int main()
{
double sp1,sp2;
int u,v,d;
while(~scanf("%d%d",&n,&m))
{
init();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&d);
if(W[u][v]>d&&u!=v)
W[u][v]=W[v][u]=d;
}
dijk();
scanf("%lf%lf",&sp1,&sp2);
if(dis[n-1]>=INF)
printf("There is a tie!\n");
else
{
printf("%.1lf %.1lf\n",(double)dis[n-1]/sp1+10.00,(double)dis[n-1]/sp2);
}
}
return 0;
}
最短路<dijk>的更多相关文章
- Alice拜年 模板题 /// 最短路Dijk oj1344
题目大意: 大年初一,Alice带上拜年礼物去给N-1位亲朋好友长辈拜年,亲友真多啊,是个大家族.由于Alice才2岁,力气不大,每次只能拿一份礼物,拜完年之后,要回家取第二份礼物,然后去下一家拜年( ...
- poj 1062 昂贵的聘礼(最短路 dijk+枚举)
终于A 了,这题做着真麻烦 题目:http://poj.org/problem?id=1062 dijk 一般用于正权有向图 此题的关键在于等级限制的处理,最好的办法是采用枚举,即假设酋长等级为5,等 ...
- POJ-1135 Domino Effect---最短路Dijk
题目链接: https://vjudge.net/problem/POJ-1135 题目大意: 有N个关键的多米诺骨牌,这些牌通过一些路径相连接,这些路径是由一排其他骨牌构成的.已知每一条路径上的骨牌 ...
- 【CodeForces 602C】H - Approximating a Constant Range(dijk)
Description through n) and m bidirectional railways. There is also an absurdly simple road network — ...
- Choose the best route(最短路)dijk
http://acm.hdu.edu.cn/showproblem.php?pid=2680 Choose the best route Time Limit: 2000/1000 MS (Java/ ...
- [BZOJ1579] [Usaco2009 Feb]Revamping Trails 道路升级(分层图最短路 + 堆优化dijk)
传送门 dis[i][j]表示第i个点,更新了j次的最短路 此题不良心,卡spfa #include <queue> #include <cstdio> #include &l ...
- *HDU3339 最短路+01背包
In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- *HDU1142 最短路+记忆化dfs
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 2851(最短路)
点击打开链接 竟然是最短路!!!! 藏的好深啊 /* 求从路1走到路i的最小危险值, 给出n条路的起点和终点,当i,j两路有重合的,我们使map[i][j]=v[j]: 把路当作最短路中的点,如果有重 ...
随机推荐
- Markdown 基础
How to use Markdown H1 text. H2 text. H3 text. H4 text. H5 text. H6 text. Text This is italic text. ...
- Android ListView 无法响应onItemClick事件
当在布局文件中加入了Button.ImageButton.CheckBox.RadioButton等控件(也就是Button或者Checkable的子类控件)的时候,listView是无法响应onIt ...
- Delphi用ADOquery主从表例子(转)
http://blog.csdn.net/kandy_zheng/article/details/1639184 在sql server 的northwide 中建立主表 create table s ...
- HDU 1009 FatMouse' Trade(贪心)
FatMouse' Trade Problem Description FatMouse prepared M pounds of cat food, ready to trade with the ...
- Ubuntu环境变量——系统变量和用户变量
系统变量: 对所有用户有效果 /etc/profile /etc/environment 两个命令只用一个就可以,原则上是重启后修改生效,但是经过验证可以通过执行以下命令实现: source /etc ...
- .Net Core 第三方工具包整理
本地日志[NLog.Extensions.Logging]:https://github.com/NLog/NLog.Extensions.Logging
- CevaEclipse - 编译器attribute扩展
1.函数与变量的 Section Attribute void foobar (void) __attribute__ ((section (".CSECT mmm"))); vo ...
- jQuery版本冲突解决办法
<!-- 引入1.6.4版的jq --> <script src="<a href="http://ajax.googleapis.com/ajax/lib ...
- java.util.Iterator
public interface Iterator<E>: 对 collection 进行迭代的迭代器. 方法摘要: boolean hasNext() 如果仍有元素可以迭代,则返回 tr ...
- 引用WCF地址报下载“https://xxx:8004/TerminalHandler.svc?disco”时出错问题
服务发布了wcf服务后,在客户端引用发现出现以下错误 - 来自“DISCO 文档”的报告是“下载“https://servername:8004/TerminalHandler.svc?disco”时 ...