题目链接:UVA1001

题意:在一个巨大奶酪中的A要以最短的时间与B相遇。在奶酪中走一米的距离花费的时间是10s,而奶酪中有许多洞,穿过这些洞的时间是0s。给出A、B以及各个洞的坐标,求最短的时间。

三维??乖乖,这怎么用最短路算法。在搜了题解后才知道可以编号压缩成二维啊,这操作骚气,实在想不出来啊!!

思路:将起点,终点,各个洞进行编号看成一个一个的点,写一个函数求出各个点之间的距离(即边的权值),在运用dijstra或Floyd算法就可以了。Ps:求距离的时候可以将各个点看成一个一个的球,距离就是两球心之间的距离减去两个球的半径和。

数据类型要用double,WA到吐得节奏。

Floyd方法:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <queue>
#include <map>
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin) using namespace std;
typedef long long ll;
const int maxn = ;
int n;
double d[maxn][maxn];
struct H
{
double x;
double y;
double z;
double r;
}hole[maxn]; double dist(H &a,H &b)
{
double x = (a.x-b.x)*(a.x-b.x);
double y = (a.y-b.y)*(a.y-b.y);
double z = (a.z-b.z)*(a.z-b.z);
if(sqrt(x+y+z) - a.r - b.r > 0.0)
return sqrt(x+y+z) - a.r - b.r;
else
return 0.0;
} int main()
{
// FRE();
int cnt = ;
while(scanf("%d",&n) && n != -)
{
for(int i = ; i < n; i++)
{
scanf("%lf%lf%lf%lf",&hole[i].x,&hole[i].y,&hole[i].z,&hole[i].r);
}
scanf("%lf%lf%lf",&hole[n].x,&hole[n].y,&hole[n].z);
scanf("%lf%lf%lf",&hole[n+].x,&hole[n+].y,&hole[n+].z); hole[n].r = hole[n+].r = ; for(int i = ; i <= n+; i++)
for(int j = ; j <= n+; j++)
{
if(i == j)
d[i][j] = ;
else
d[i][j] = INF;
}
for(int i = ; i < n+; i++)
for(int j = ; j < n+; j++)
if(i != j) d[i][j] = dist(hole[i],hole[j]); for(int k = ; k < n+; k++)
for(int i = ; i < n+; i++)
for(int j = ; j < n+; j++)
{
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
}
d[n][n+] *= ;
printf("Cheese %d: Travel time = %.0f sec\n",++cnt,d[n][n+]);
}
return ;
}

Dijkstra邻接矩阵方法:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define FRE() freopen("in.txt","r",stdin)
#define INF 0x3f3f3f3f using namespace std;
const int maxn = ;
double x[maxn],y[maxn],z[maxn],r[maxn];
double d[maxn],vis[maxn];
double mp[maxn][maxn];
int n; double dist(int i,int j)
{
double tx = (x[i]-x[j])*(x[i]-x[j]);
double ty = (y[i]-y[j])*(y[i]-y[j]);
double tz = (z[i]-z[j])*(z[i]-z[j]);
double res = sqrt(tx + ty + tz) - r[i] - r[j];
if(res > )
return res;
else
return ;
} void Dij()
{
memset(vis,,sizeof(vis));
for(int i = ; i <= n+; i++)
d[i] = INF;
d[] = ;
for(int i = ; i <= n+; i++)
{
int u;double mmin = INF;
for(int i = ; i <= n+; i++)
{
if(!vis[i] && d[i] < mmin)
{
mmin = d[i];
u = i;
}
}
if(u == n+) return;
vis[u] = ;
for(int i = ; i <= n+; i++)
{
d[i] = min(d[i], d[u]+mp[u][i]);
}
} } int main()
{
//FRE();
int cnt = ;
while(scanf("%d",&n) && n != -)
{
for(int i = ; i <= n; i++)
{
scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
}
scanf("%lf%lf%lf",&x[],&y[],&z[]);r[] = ;
scanf("%lf%lf%lf",&x[n+],&y[n+],&z[n+]);r[n+] = ; for(int i = ; i <= n+; i++)
{
for(int j = ; j <= n+; j++)
mp[i][j] = dist(i,j);
}
Dij();
printf("Cheese %d: Travel time = %.0f sec\n",++cnt,d[n+]*); }
return ;
}

Dijkstra优先队列方法:vector数组的清空啊,别问我是怎么知道的!!!!!!

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#define FRE() freopen("in.txt","r",stdin)
#define INF 0x3f3f3f3f using namespace std;
typedef pair<double,int> P;
const int maxn = ;
struct H
{
double x,y,z;
double r;
}hole[maxn];
struct edge
{
int to;
double cost;
edge(int t,double c):to(t),cost(c){}
};
vector<edge> g[maxn];
double d[maxn]; double dist(H &a,H &b)
{
double x = (a.x - b.x)*(a.x - b.x);
double y = (a.y - b.y)*(a.y - b.y);
double z = (a.z - b.z)*(a.z - b.z);
double res = sqrt(x + y + z) - a.r - b.r;
if(res > )
return res;
else
return ;
} int main()
{
//FRE();
int cnt = ;
int n;
while(scanf("%d",&n) && n != -)
{
for(int i = ; i <= n; i++)
{
scanf("%lf%lf%lf%lf",&hole[i].x,&hole[i].y,&hole[i].z,&hole[i].r);
}
scanf("%lf%lf%lf",&hole[].x,&hole[].y,&hole[].z); hole[].r = ;
scanf("%lf%lf%lf",&hole[n+].x,&hole[n+].y,&hole[n+].z);hole[n+].r = ;
for(int i = ; i < n+; i++) g[i].clear();
for(int i = ; i < n+; i++)
{
for(int j = i+; j < n+; j++)
{
double t = dist(hole[i],hole[j]);
g[i].push_back(edge(j,t));
g[j].push_back(edge(i,t));
}
} for(int i = ; i <n+; i++)
d[i] = INF;
d[] = ;
priority_queue<P, vector<P>, greater<P> > que;
que.push(P(,));
while(!que.empty())
{
P p = que.top();
que.pop();
int v = p.second;
if(d[v] < p.first) continue;
for(int i = ; i < g[v].size(); i++)
{
edge ee = g[v][i];
if(d[ee.to] > d[v] + ee.cost)
{
d[ee.to] = d[v] + ee.cost;
que.push(P(d[ee.to], ee.to));
}
}
}
printf("Cheese %d: Travel time = %.0f sec\n",++cnt,d[n+]*);
}
return ;
}

UVA1001 Say Cheese(Dijkstra或Floyd)的更多相关文章

  1. 算法学习笔记(三) 最短路 Dijkstra 和 Floyd 算法

    图论中一个经典问题就是求最短路.最为基础和最为经典的算法莫过于 Dijkstra 和 Floyd 算法,一个是贪心算法,一个是动态规划.这也是算法中的两大经典代表.用一个简单图在纸上一步一步演算,也是 ...

  2. (最短路径算法整理)dijkstra、floyd、bellman-ford、spfa算法模板的整理与介绍

    这一篇博客以一些OJ上的题目为载体.整理一下最短路径算法.会陆续的更新... 一.多源最短路算法--floyd算法 floyd算法主要用于求随意两点间的最短路径.也成最短最短路径问题. 核心代码: / ...

  3. 最短路(Dijkstra,Floyd,Bellman_Ford,SPFA)

    当然,这篇文章是借鉴大佬的... 最短路算法大约来说就是有4种——Dijkstra,Floyd,Bellman_Ford,SPFA 接下来,就可以一一看一下... 1.Dijkstra(权值非负,适用 ...

  4. Uva1001 Say Cheese Floyd

    题意:一个无限大的奶酪里有n个球形的洞,在洞内可以瞬移,不然每一个单位要用10sec,现在给定起始点和结束点,问最短需要耗时多久? 思路:把球形的洞当做是节点,两点之间的距离是两者球心的距离减去两者的 ...

  5. UVA1001 Say Cheese (dijkstra)

    如果没有洞,那么任意两点的最短距离就是直线距离,洞里是瞬间的,所以看成一个点就行了(其实点也可以当作半径为0的洞来处理),洞到洞的最短距离都是圆心距离减去半径.剩下的就是求单源最短路径,是完全图,用不 ...

  6. UVa 1001 奶酪里的老鼠(Dijkstra或Floyd)

    https://vjudge.net/problem/UVA-1001 题意:一个奶酪里有n个洞,老鼠在奶酪里的移动速度为10秒一个单位,但是在洞里可以瞬间移动.计算出老鼠从A点到达O点所需的最短时间 ...

  7. dijkstra,bellman-ford,floyd分析比较

    http://www.cnblogs.com/mengxm-lincf/archive/2012/02/11/2346288.html 其实我一直存在疑惑是什么导致dijkstra不能处理负权图? 今 ...

  8. 最短路 dijkstra and floyd

    二:最短路算法分析报告 背景 最短路问题(short-path problem):若网络中的每条边都有一个数值(长度.成本.时间等),则找出两节点(通常是源节点和阱节点)之间总权和最小的路径就是最短路 ...

  9. 最短路知识点总结(Dijkstra,Floyd,SPFA,Bellman-Ford)

    Dijkstra算法: 解决的问题: 带权重的有向图上单源最短路径问题.且权重都为非负值.如果采用的实现方法合适,Dijkstra运行时间要低于Bellman-Ford算法. 思路: 如果存在一条从i ...

随机推荐

  1. Cocos2d-x v3.0正式版尝鲜体验【2】 Android平台移植

    今天没事又尝试了下3.0正式版关于Android平台的移植,把新建的项目移植了下.过程仅用了十分钟左右,什么概念?! 好吧,事实上我想说,这个版本号真的移植非常轻松啊,只是还没加上其它东西,只是就眼下 ...

  2. POJ2393 Yogurt factory 【贪心】

    Yogurt factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6821   Accepted: 3488 De ...

  3. POJ 2007 Scrambled Polygon(简单极角排序)

    水题,根本不用凸包,就是一简单的极角排序. 叉乘<0,逆时针. #include <iostream> #include <cstdio> #include <cs ...

  4. 软件project—思考项目开发那些事(一)

    阅读文件夹: 1.背景 2.项目管理,质量.度量.进度 3.软件开发是一种设计活动而不是建筑活动 4.高速开发(简单的系统结构与复杂的业务模型) 5.技术人员的业务理解与产品经理的业务理解的终于业务模 ...

  5. CSS里面position:relative与position:absolute 区别

    position:absolute这个是绝对定位:是相对于浏览器的定位.比如:position:absolute:left:20px;top:80px; 这个容器始终位于距离浏览器左20px,距离浏览 ...

  6. JSP-Runoob:JSP 指令

    ylbtech-JSP-Runoob:JSP 指令 1.返回顶部 1. JSP 指令 JSP指令用来设置整个JSP页面相关的属性,如网页的编码方式和脚本语言. 语法格式如下: <%@ direc ...

  7. day03_12/13/2016_bean属性的设置之setter方法注入

  8. 【转】Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  9. [ HAOI 2008 ] 玩具取名

    \(\\\) \(Description\) 在一个只有\(W,I,N,G\)的字符集中,给出四个字符的若干映射,每个映射为一个字符映射到两个字符,现给你一个假定由一个字符经过多次映射产生的字符串,问 ...

  10. Python--10、生产者消费者模型

    生产者消费者模型(★) 平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度.程序中有两类角色:生产数据.消费数据实现方式:生产->队列->消费. 通过一个容器来解决生产者和消费 ...