最短路<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]: 把路当作最短路中的点,如果有重 ...
随机推荐
- java ee开发报错
七月 26, 2015 9:57:52 下午 org.apache.coyote.AbstractProtocol destroy信息: Destroying ProtocolHandler [&qu ...
- JS 之完美运动框架 如何同时改变元素多个属性?
正如改变一个属性的方法,可以使用已经写好的运动函数如move(obj,attr,target,fn);我们可能会想这样做, 调用两次运动函数,如同时改变宽和高,move(obj,'width',tar ...
- Developing a Custom Membership Provider from the scratch, and using it in the FBA (Form Based Authentication) in SharePoint 2010
//http://blog.sharedove.com/adisjugo/index.php/2011/01/05/writing-a-custom-membership-provider-and-u ...
- HTML URL编码参考(转载)
URL编码(URL encoding)的作用是将字符转化为可在因特网上安全传输的格式.URL——统一资源定位符Web浏览器通过URL从Web服务器上请求页面.URL就是网页的地址,如:http://w ...
- APP测试--功能测试
1.1 了解需求 这一点,不但是功能测试,是所有测试都需要的第1步.通过需求文档,与产品经理的沟通,与开发的沟通,用户的使用习惯等各方法,了解APP的需求. 1.2 编写测试用例 当然之前可能是测试计 ...
- Windows kernel pool 初探(2014.12)
Windows kernel pool 1. 简介 Kernel pool类似于Windows用户层所使用Heap,其为内核组件提供系统资源.在系统初始化的时候,内存管理模块就创建了pool. 严格的 ...
- Webdriver如何解决页面元素过期:org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up
当运行Webdriver时抛出如下异常:org.openqa.selenium.StaleElementReferenceException: Element not found in the cac ...
- 二十二、oracle pl/sql分类二 函数
函数用于返回特定的数据,当建立函数时,在函数头部必须包含return子句.而在函数体内必须包含return语句返回的数据.我们可以使用create function来建立函数. 1).接下来通过一个案 ...
- Centos6.6升级python版本
centos原生python为2.6.6,可以通过下面的命令查看 #python -V Python 注:在安装新版本前,请先安装zlib\openssl组件,如果你确认你用不到这个,也可以不装 需要 ...
- HDU - 1045 Fire Net(搜索)
Description Suppose that we have a square city with straight streets. A map of a city is a square bo ...