题目链接:UVA1001
题意:在一个巨大奶酪中的A要以最短的时间与B相遇。在奶酪中走一米的距离花费的时间是10s,而奶酪中有许多洞,穿过这些洞的时间是0s。给出A、B以及各个洞的坐标,求最短的时间。
三维??乖乖,这怎么用最短路算法。在搜了题解后才知道可以编号压缩成二维啊,这操作骚气,实在想不出来啊!!
思路:将起点,终点,各个洞进行编号看成一个一个的点,写一个函数求出各个点之间的距离(即边的权值),在运用dijstra或Floyd算法就可以了。Ps:求距离的时候可以将各个点看成一个一个的球,距离就是两球心之间的距离减去两个球的半径和。
数据类型要用double,WA到吐得节奏。
Floyd方法:
#include
#include
#include
#include
#include
#include
#include
#include
Dijkstra邻接矩阵方法:
#include
#include
#include
#include
#include
#include
#include
#define FRE() freopen("in.txt","r",stdin)
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 300;
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 > 0)
return res;
else
return 0;
}
void Dij()
{
memset(vis,0,sizeof(vis));
for(int i = 0; i <= n+1; i++)
d[i] = INF;
d[0] = 0;
for(int i = 0; i <= n+1; i++)
{
int u;double mmin = INF;
for(int i = 0; i <= n+1; i++)
{
if(!vis[i] && d[i] < mmin)
{
mmin = d[i];
u = i;
}
}
if(u == n+1) return;
vis[u] = 1;
for(int i = 0; i <= n+1; i++)
{
d[i] = min(d[i], d[u]+mp[u][i]);
}
}
}
int main()
{
//FRE();
int cnt = 0;
while(scanf("%d",&n) && n != -1)
{
for(int i = 1; i <= n; i++)
{
scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);
}
scanf("%lf%lf%lf",&x[0],&y[0],&z[0]);r[0] = 0;
scanf("%lf%lf%lf",&x[n+1],&y[n+1],&z[n+1]);r[n+1] = 0;
for(int i = 0; i <= n+1; i++)
{
for(int j = 0; j <= n+1; j++)
mp[i][j] = dist(i,j);
}
Dij();
printf("Cheese %d: Travel time = %.0f sec\n",++cnt,d[n+1]*10);
}
return 0;
}
Dijkstra优先队列方法:vector数组的清空啊,别问我是怎么知道的!!!!!!
#include
#include
#include
#include
#include
#include
#include
#define FRE() freopen("in.txt","r",stdin)
#define INF 0x3f3f3f3f
using namespace std;
typedef pair P;
const int maxn = 300;
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 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 > 0)
return res;
else
return 0;
}
int main()
{
//FRE();
int cnt = 0;
int n;
while(scanf("%d",&n) && n != -1)
{
for(int i = 1; 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[0].x,&hole[0].y,&hole[0].z); hole[0].r = 0;
scanf("%lf%lf%lf",&hole[n+1].x,&hole[n+1].y,&hole[n+1].z);hole[n+1].r = 0;
for(int i = 0; i < n+2; i++) g[i].clear();
for(int i = 0; i < n+2; i++)
{
for(int j = i+1; j < n+2; 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 = 0; i , greater > que;
que.push(P(0,0));
while(!que.empty())
{
P p = que.top();
que.pop();
int v = p.second;
if(d[v] < p.first) continue;
for(int i = 0; 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+1]*10);
}
return 0;
}