UVA 1001 Say Cheese(dijkstra算法)
2015-11-11 22:39
411 查看
题意:
在实体空间中给出若干个空心的球,在球中可以瞬间移动, 在实体中移动花费与距离成正比的的时间,给出两点求到达的最短时间。
解题思路:
花样建图, 把起点和终点作为半径为0的球加入图中,使用dijkstra算法一搞就好啦!
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3442
在实体空间中给出若干个空心的球,在球中可以瞬间移动, 在实体中移动花费与距离成正比的的时间,给出两点求到达的最短时间。
解题思路:
花样建图, 把起点和终点作为半径为0的球加入图中,使用dijkstra算法一搞就好啦!
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3442
Memory: 0 KB | Time: 3 MS | |
Language: C++ 4.8.2 | Result: Accepted |
#include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cctype> #include<list> #include<iostream> #include<map> #include<queue> #include<set> #include<stack> #include<vector> using namespace std; #define FOR(i, s, t) for(int i = (s) ; i <= (t) ; ++i) #define REP(i, n) for(int i = 0 ; i < (n) ; ++i) int buf[10]; inline long long read() { long long x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } inline void writenum(int i) { int p = 0; if(i == 0) p++; else while(i) { buf[p++] = i % 10; i /= 10; } for(int j = p - 1 ; j >= 0 ; --j) putchar('0' + buf[j]); } /**************************************************************/ #define MAX_N 110 const int INF = 0x3f3f3f3f; int n; typedef pair<double, int> PP; priority_queue<PP, vector<PP>, greater<PP> > que; double d[MAX_N]; struct point { int x, y, z, r; }P[MAX_N]; struct node { int to, next; double w; } edge[MAX_N * MAX_N]; int head[MAX_N]; int top = 0; inline void add_edge(int u, int v, double w) { edge[top].to = v; edge[top].w = w; edge[top].next = head[u]; head[u] = top++; } double dist(point a, point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) + (a.z - b.z) * (a.z - b.z)) - a.r - b.r; } void dijkstra(int s) { d[s] = 0.0; que.push(PP(0.0, s)); while(!que.empty()) { PP p = que.top(); que.pop(); int v = p.second; if(d[v] < p.first) continue; for(int k = head[v] ; k != -1 ; k = edge[k].next) { // cout<<d[edge[k].to]<<" "<<d[v]<<" "<<edge[k].w<<endl; if(d[edge[k].to] > d[v] + edge[k].w) { d[edge[k].to] = d[v] + edge[k].w; que.push(PP(d[edge[k].to], edge[k].to)); } } } } inline void init() { for(int i = 0 ; i < MAX_N ; i ++) { d[i] = 1e10; } memset(head, -1, sizeof(head)); while(!que.empty()) que.pop(); top = 0; } int main() { int cas = 1; while(scanf("%d",&n) && n != -1) { init(); for(int i = 1 ; i <= n ; i++) { P[i].x = read(); P[i].y = read(); P[i].z = read(); P[i].r = read(); } P[0].x = read(); P[0].y = read(); P[0].z = read(); P[0].r = 0; P[n + 1].x = read(); P[n + 1].y = read(); P[n + 1].z = read(); P[n + 1].r = 0; // cout<<endl<<endl; for(int i = 0 ; i <= n + 1 ; i++) { for(int j = 0 ; j <= n + 1 ; j++) { if(i != j) { double w = dist(P[i], P[j]); if(w < 1e-8) w = 0; add_edge(i ,j, w); // cout<<i<<" "<<j<<" "<<w<<endl; } } } // cout<<endl<<endl; // for(int i = 0 ; i <= n + 1 ; i++) // { // for(int k = head[i] ; k != -1 ; k = edge[k].next) // { // cout<<i<<" "<<edge[k].to<<" "<<edge[k].w<<endl; // } // } dijkstra(0); // cout<<endl<<endl; // for(int i = 0 ; i <= n + 1 ; i++) // { // cout<<d[i]<<endl; // } int ans = (int)(d[n + 1] * 10 + 0.5); printf("Cheese %d: Travel time = %d sec\n", cas++, ans); } return 0; }
相关文章推荐
- NSUserDefaults和dispatch_once的注意点
- hdu1286 找朋友 (欧拉函数+素数筛法)
- Hello, world!
- 系统自带的UIBarButtonSystemItem样式
- 面向对象程序设计上机练习二(函数模板)
- iOS开发见闻-第6期
- 数据集成实例
- Struts2中的校验框架
- CSS3--阴影,渐变,背景图片
- spring管理bean的原理
- Java基础数据类型转换
- 详解Swift中的iOS设计模式
- Js apply方法详解
- poj 1703 Find them, Catch them 并查集
- beego框架——快速开发go应用的http框架
- iOS开发 - Quartz2D绘图
- 按键精灵-如何不需要路径执行.exe文件
- C#的ConsoleKeyInfo
- Java Web开发模式
- nyoj 找点 891 (贪心)