poj 1556 zoj1721 BellmanFord 最短路+推断直线相交
http://poj.org/problem?id=1556
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 6120 | Accepted: 2455 |
Description
from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.
Input
2
4 2 7 8 9
7 3 4.5 6 7
The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways
in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.
Output
blanks.
Sample Input
1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1
Sample Output
10.00
10.06
Source
開始的时候真的是二逼了,
1、推断相交的函数写错了,我竟然推断的是是不是跟源点和终点的直线相交。。。二逼啊,,,
2、然后改了之后还wa,由于推断里少了个!,,,,没取反,,,
3、极限的点,比方每道墙的最上沿和最下沿,这两个点不可达,就是说从源头到终点不能经过这两个点,開始的时候没排除,尽管那样的话也能AC,还是题目数据太弱了啊
我自己写的推断直线相交的模板:
/*==========================================================*\
|| 推断点在直线上或直线相交
1、函数值为0,表示在直线上;
2、test(a,b,t1)*test(a,b,t2)<0表示直线ab和直线t1t2相交
\*==========================================================*/
double test(Point a,Point b, Point t)
{
return (b.y-a.y)*(t.x-b.x)-(b.x-a.x)*(t.y-b.y);
}
思路还是比較顺的,就是最短路+推断直线相交
贴代码:
#include<cstdio>
#include<cstring>
#include <string>
#include <map>
#include <iostream>
#include <cmath>
using namespace std;
#define INF 10000
const double eps=1e-6; const int MAXN = 1011;
#define Max(a,b) (a)>(b)?(a):(b)
int cntp;
int wn;
struct Point{
Point(double x=0,double y=0):x(x),y(y){}
double x,y;
int id;
}p[MAXN];
struct Wall{
double s1,e1;
double s2,e2;
double s3;
}w[20];//=0~~=wn
double e[MAXN][MAXN],dist[MAXN]; double dis(Point a, Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} void init()
{
cntp=1;
for(int i=0;i<=MAXN;i++)
for(int j=0;j<=MAXN;j++)
{
if(i == j)e[i][j]=0;
else e[i][j]=INF;
}
p[0].x=0,p[0].y=5,p[0].id=0;
for(int i=0;i<=MAXN;i++)dist[i]=INF;
} double test(Point a,Point b, Point t)
{
return (b.y-a.y)*(t.x-b.x)-(b.x-a.x)*(t.y-b.y);
} bool Judge(Point a, Point b)
{
if(a.id>b.id)
{
Point t=a;
a=b;
b=t;
}
//int flag=1;
if(a.id>0)
if(a.y -0.0 <=eps||10.0-a.y <=eps)
return 0;
if(b.id<cntp-1)
if(b.y-0.0<=eps || 10.0-b.y<=eps)
return 0; for(int i=a.id+1;i<b.id;i++)
{
Point p1(w[i].s1,w[i].e1),p2(w[i].s1,w[i].s2),p3(w[i].s1,w[i].e2),p4(w[i].s1,w[i].s3); if(!(
test(a,b,p1)*test(a,b,p2)<0 ||
test(a,b,p3)*test(a,b,p4)<0)
)return 0;
}
/*for(int i=a.id+1;i<b.id;i++)
{
if(!(
(w[i].e1<5.0&&w[i].s2>5.0)
|| (w[i].e2<5.0&&w[i].s3>5.0)
)
)return 0;
}*/
return 1;
} void Build()
{
for(int i=0;i<cntp;i++)
{
for(int j=i+1;j<cntp;j++)
{
//if(i == j)continue;
if(p[i].id == p[j].id)continue; if(Judge(p[i],p[j]))
{
e[i][j]=min(e[i][j],dis(p[i],p[j]));
}
}
}
} void Bellman(int v0)
{
int n=cntp;
for(int i=0;i<cntp;i++)
{
dist[i]=e[v0][i];
//if(i!=v0 && dist[i]<INF)
}
for(int k=2;k<n;k++)
{
for(int u=0;u<n;u++)
{
if(u!=v0)
{
for(int j=0;j<n;j++)
{
if(e[j][u]!=INF && dist[j]+e[j][u]<dist[u])
{
dist[u]=dist[j]+e[j][u];
}
}
}
}
}
} int main()
{
// freopen("poj1556.txt","r",stdin);
while(~scanf("%d",&wn) && ~wn)
{
init();
for(int i=1;i<=wn;i++)
{
scanf("%lf%lf%lf%lf%lf",&w[i].s1,&w[i].e1,&w[i].s2,&w[i].e2,&w[i].s3);
p[cntp].id=p[cntp+1].id=p[cntp+2].id=p[cntp+3].id=p[cntp+4].id=p[cntp+5].id=i;
p[cntp].x=p[cntp+1].x=p[cntp+2].x=p[cntp+3].x=p[cntp+4].x=p[cntp+5].x=w[i].s1;
p[cntp].y=0.0,p[cntp+1].y=w[i].e1,p[cntp+2].y=w[i].s2,p[cntp+3].y=w[i].e2,p[cntp+4].y=w[i].s3,p[cntp+5].y=10.0;
////////////////
//e[cntp+1][cntp+2]=w[i].s2-w[i].e1;
// e[cntp+3][cntp+4]=w[i].s3-w[i].e2;
cntp+=6;
}
p[cntp].x=10.0,p[cntp].y=5.0,p[cntp].id=++wn;
cntp++;
//if()
Build();
Bellman(0);
printf("%.2lf\n",dist[cntp-1]);
///////////////////
// for(int i=0;i<cntp;i++)
// printf("%d %lf\n",i,dist[i]);
}
return 0;
}
poj 1556 zoj1721 BellmanFord 最短路+推断直线相交的更多相关文章
- POJ 1039 Pipe【经典线段与直线相交】
链接: http://poj.org/problem?id=1039 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- 最短路+线段交 POJ 1556 好题
// 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- 直线相交 POJ 1269
// 直线相交 POJ 1269 // #include <bits/stdc++.h> #include <iostream> #include <cstdio> ...
- 判断线段和直线相交 POJ 3304
// 判断线段和直线相交 POJ 3304 // 思路: // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. #include <cstdio ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 1269 Intersecing Lines (直线相交)
题目: Description We all know that a pair of distinct points on a plane defines a line and that a pair ...
随机推荐
- ios 多线程开发(一)简介
简介 线程是在一个程序中并发的执行代码的方法之一.虽然有一些新的技术(operations, GCD)提供了更先进高效的并发实现,OS X和iOS同时也提供了创建和维护线程的接口. 这里将要介绍线程相 ...
- zoj 1738 - Lagrange's Four-Square Theorem
称号:四方形定理.输出可以表示为一个数目不超过四个平方和表示的数. 分析:dp,完全背包.背包分割整数.可用一维分数计算,它也可以被写为一个二维团结. 状态:设f(i,j,k)为前i个数字,取j个数字 ...
- 【转】Directx11 HelloWorld之HLSL的Effect框架的使用
最近尝试用了下Directx下的Effect框架,作为一初学者初学者,说下为什么我们要使用Effect框架及其好处吧. 首先Effect最大好处的就是简单,使得编写Shader绘制的程序工作量大大下降 ...
- .NET 4 并行(多核)编程系列之四 Task的休眠
原文:.NET 4 并行(多核)编程系列之四 Task的休眠 .NET 4 并行(多核)编程系列之四 Task的休眠 前言:之前的几篇文章断断续续的介绍了Task的一些功能:创建,取消.本篇介绍Tas ...
- C/S通信模型和相关技术要点
差点儿全部的项目中,都会涉及到client和服务端.而client与server之间的通信又是一个非经常见但又有须要问题的技术问题. 首先,连接方式有长连接和短连接.先看看概念. 长连接短连接仅仅是一 ...
- 使用WindowManager添加您自己的自定义视图
在写手机卫士的时候,用户拨打|接听电话须要显示号码归属地,然后出现了一些异常,在此留下记号.希望对麻友们有帮助: BUG教程 在使用 view = View.inflate(this, R.layou ...
- Yaha,Yaho
Yaha: Yaho: 听雪楼上听雪落,雪落无声空余楼. 同样的地方,一坐三年多,人走楼空,回顾空留. 自己非常白痴地画着苹果,非常嗨森地逗自己玩. 这两层精致的书库是大学里面能容纳我的地方(ABC的 ...
- 采用curl库
Windows通过使用curl库: 到http://curl.haxx.se/下了个curl的源代码下来,源代码是用VC6编译的,我在VS2005下又一次进行编译.竟然仅仅有一个警告. cUrl的实现 ...
- SGU 200. Cracking RSA(高斯消元+高精度)
标题效果:鉴于m整数,之前存在的所有因素t素数.问:有多少子集.他们的产品是数量的平方. 解题思路: 全然平方数就是要求每一个质因子的指数是偶数次. 对每一个质因子建立一个方程. 变成模2的线性方程组 ...
- Codefoces 432 C. Prime Swaps
哥德巴赫猜想: 任一大于2的偶数,都可表示成两个素数之和. 任一大于5的整数都可写成三个质数之和. 贪心取尽可能大的素数..... C. Prime Swaps time limit per test ...