题意:

      求s到t的最短路,如果路径相同求那么要求另一个权值尽可能的小.

思路:

      水题,就是spfa的比较那个地方多了一个可以更新的机会,当(s_x[xin] > s_x[tou] + E[k].cost || s_x[xin] == s_x[tou] + E[k].cost && s_t[xin] > s_t[tou] + 1) 时更新就行了..


#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h> #define N_node 100 + 5
#define N_edge 20000 + 500
#define INF 1000000000

using namespace
std; typedef struct
{
int
to ,next;
double
cost;
}
STAR; typedef struct
{
double
x ,y;
}
NODE; STAR E[N_edge];
NODE node[N_node];
int
list[N_node] ,tot;
double
s_x[N_node];
int
s_t[N_node]; void add(int a ,int b ,double c)
{

E[++tot].to = b;
E[tot].cost = c;
E[tot].next = list[a];
list[a] = tot;
} double
abss(double x)
{
return
x > 0 ? x : -x;
}
double
minn(double x ,double y)
{
return
x < y ? x : y;
} void
SPFA(int s ,int n)
{
for(int
i = 0 ;i <= n ;i ++)
s_x[i] = INF ,s_t[i] = INF;
int
mark[N_node] = {0};
mark[s] = 1;
s_x[s] = s_t[s] = 0;
queue<int>q;
q.push(s);
while(!
q.empty())
{
int
xin ,tou;
tou = q.front();
q.pop();
mark[tou] = 0;
for(int
k = list[tou] ;k ;k = E[k].next)
{

xin = E[k].to;
if(
s_x[xin] > s_x[tou] + E[k].cost || abss(s_x[xin] - s_x[tou] + E[k].cost) < 1e-6 && s_t[xin] > s_t[tou] + 1)
{

s_x[xin] = s_x[tou] + E[k].cost;
s_t[xin] = s_t[tou] + 1;
if(!
mark[xin])
{

mark[xin] = 1;
q.push(xin);
}
}
}
}
return ;
} int main ()
{
int
n ,i ,j;
double
d;
while(~
scanf("%d %lf" ,&n ,&d))
{
for(
i = 1 ;i <= n ;i ++)
scanf("%lf %lf" ,&node[i].x ,&node[i].y);
memset(list ,0 ,sizeof(list));
tot = 1;
for(
i = 1 ;i <= n ;i ++)
for(
j = i + 1 ;j <= n ;j ++)
{
double
dis = pow(node[i].x - node[j].x ,2.0) + pow(node[i].y - node[j].y,2.0);
if(
dis <= d * d)
{

add(i ,j ,sqrt(dis));
add(j ,i ,sqrt(dis));
}
} int
s = 0 ,t = n + 1;
for(
i = 1 ;i <= n ;i ++)
{
double
dis = pow(node[i].x,2.0) + pow(node[i].y ,2.0);
if(
pow(d + 7.5 ,2.0) >= dis)
add(s ,i ,sqrt(dis) - 7.5);
dis = minn(50 - abss(node[i].x) ,50 - abss(node[i].y));
if(
dis <= d) add(i ,t ,dis);
}
if(
d >= 50 - 7.5)
add(s ,t ,50 - 7.5);
SPFA(s ,t);
if(
s_x[t] == INF)
printf("can't be saved\n");
else

printf("%.2lf %d\n" ,s_x[t] ,s_t[t]);
}
return
0;
}

hdu1245 两个权值的最短路的更多相关文章

  1. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  2. hdu-3790 最短路径问题---dijkstra两重权值

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3790 题目大意: 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到 ...

  3. 51nod1459(带权值的dijkstra)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1459 题意:中文题诶- 思路:带权值的最短路,这道题数据也没 ...

  4. NOIP 提高组 2014 联合权值(图论???)

    传送门 https://www.cnblogs.com/violet-acmer/p/9937201.html 题解: 相关变量解释: int n; int fa[maxn];//fa[i] : i的 ...

  5. BZOJ2733/LG3324 「HNOI2014」永无乡 权值线段树合并

    问题描述 BZOJ2733 LG3224 题解 对于每个结点建立一棵权值线段树. 查询操作就去查询第 \(k\) 大,合并操作就合并两颗权值线段树. 并查集维护连通性. 同时 STO hkk,zcr, ...

  6. P1351 联合权值[鬼畜解法]

    题目描述 无向连通图 G 有 n 个点,n−1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi​,每条边的长度均为 1.图上两点 (u,v) 的距离定义为 u 点到 v 点的最短距离 ...

  7. Wormholes 最短路判断有无负权值

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  8. poj 1789 每个字符串不同的字母数代表两个结点间的权值 (MST)

    题目大意是就是给出n个长度为7的字符串,每个字符串代表一个车,定义车的距离是两个字符串间不同字母的个数,题目要求的数不同的车的距离的最小值,即所求的就是最小生成树 Sample Input 4aaaa ...

  9. UVA 12661(动态权值+最短路,dij)

    题意:赛车背景,给你n个节点,m条边的图以及起点和终点:其中每条边的信息包括u(起点),v(终点),a(开启的时间),b(关闭的时间),d(通过这条道路的时间):求最短通过的时间,其中车在进的时候,保 ...

随机推荐

  1. PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列

    Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...

  2. Bullet碰撞检测

    DBVT 在bullet 引擎中是很基础且重要的一个数据结构,本质上是一个可以动态更新的AABB树. 碰撞响应的分析 约束分类:可积约束,不可积约束 ,摩擦力(见[1]第四章) 整个bullet在动力 ...

  3. 开发过程中遇到的js知识点总结,面试题等,持续更新

     1.Object.freeze() 方法用于冻结一个对象,即将对象设置为不可扩展.将对象的所有自有的属性和方法(包括Symbol值的属性和方法)配置为不可配置,不可写. Object.freeze( ...

  4. 爬虫必知必会(5)_scrapy框架_基础

    一.移动端数据的爬取 基于某一款抓包工具,fiddler,青花瓷,miteproxy fillder进行一个基本的配置:tools->options->connection->all ...

  5. python基础学习之列表的功能方法

    列表:list 格式 li = [1,2,3,4,5,6] 列表内部随意嵌套其他格式:字符串.列表.数字.元组.字典. 列表内部有序,且内容可更改 a = [1,2,3,4]    a[0] = 5  ...

  6. python的类的实际联系--烤地瓜和搬家具

    #coding:utf-8 2 class SweetPotato(): 3 def __init__(self): 4 #先初始化对象 5 self.cook_time = 0 6 self.coo ...

  7. 让你弄懂js中的闭包

    目录 闭包 闭包如何产生 闭包是什么 常见的闭包 闭包的作用 闭包的生命周期 闭包的应用 闭包的缺点 内存泄露 内存溢出 闭包面试题 闭包 之前在我执行上下文执行上下文栈这篇文章中,出现了这样一个题目 ...

  8. 攻防世界 reverse crazy

    crazy 百越杯2018 查看main函数: int __cdecl main(int argc, const char **argv, const char **envp) { __int64 v ...

  9. 攻防世界 reverse secret-string-400

    secret-string-400 school-ctf-winter-2015 解压文件得到html和js文件 Task.html <html> <head> <tit ...

  10. Git代码版本控制流程

    我们的项目使用Git作为代码仓库.和版本控制工具. Git有几种Workflow,来管理代码版本变更流程,我们采用Gitflow Workflow流程. Gitflow Workflow,采用了mas ...