题目链接: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. Ubuntu13.10添加/删除PPA

    什么是ppa ppa即Personal Package Archive(个人安装包文档),简单来说就是一些个人或者团体通过独立的网站发布的第三方的软件源.将这些软件源添加到你的电脑上后,你就可以像使用 ...

  2. 从零讲Java,给你一条清晰地学习道路!该学什么就学什么!

                                             从零讲JAVA ,给你一条 清晰地学习道路!该学什么就学什么! 1.计算机基础: 1.1数据机构基础: 主要学习:1. ...

  3. Combining an audio file with video file in python

    Combining an audio file with video file in python - Stack Overflow https://stackoverflow.com/questio ...

  4. luogu1641 [SDOI2010]生成字符串

    题目大意 把$n$个$1$和$m$个$0$组成字符串,在任意的前$k$个字符中,$1$的个数不能少于$0$的个数.求这样的字符串的个数.$1\leq m\leq n\leq 1000000$. 原始模 ...

  5. JavaScript Patterns 2.1 Writing Maintainable Code

    Revisiting the code after some time has passed requires: • Time to relearn and understand the proble ...

  6. 树的遍历 迭代算法——思路:初始化stack,pop stack利用pop的node,push new node to stack,可以考虑迭代一颗树 因为后序遍历最后还要要访问根结点一次,所以要访问根结点两次是难点

    144. Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' ...

  7. MSP430:输入捕获

    在做超声模块时用到 //捕获上升沿 void Capture_Pos(void) { P2SEL |= Echo; //选择P23作为捕捉的输入端子 Timer1_A //TA1CCTL1 |=CM_ ...

  8. error: undefined reference to 'property_set (转载)

    转自:http://blog.csdn.net/u011589606/article/details/23474241 in the cpp file, please include #include ...

  9. python3+request接口自动化框架

    首次书写博客,记录下写的自动化接口框架,框架比较简单,哈哈哈,算是记录下历程把!~~~ 一.本次框架由python3.6 书写 1.准备代码环境,下载python3.6    下载地址:https:/ ...

  10. Mac OS下配置 ADB环境变量

    前提已经安装了Android sdk. 步骤打开终端Terminal, 输入open -e ~/.bash_profile, 若之前没有该文件,会自动创建.添加内容 export PATH=${PAT ...