HDU 1255(矩形面积交.)、POJ - 2031(最小生成树.)、POJ - 3268(最短路.)、POJ - 1860(最短路之Bellman_Ford算法.)

Time limit 5000 ms
Memory limit 32768 kB
OS Windows

题目:
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.

Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.
Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
Sample Input
2
5
1 1 4 2
1 3 3 7
2 1.5 5 4.5
3.5 1.25 7.5 4
6 3 10 7
3
0 0 1 1
1 0 2 1
2 0 3 1
Sample Output
7.63
0.00

代码:

/*
HDU 1255 覆盖的面积
求矩形面积交(离散化+线段树)
给定一些矩形
求被这些矩形覆盖过至少两次的区域的面积

Author:kuangbin
date:2012-8-15

*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int MAXN=2020;
struct Node
{
    int l,r;
    int c;
    double lf,rf;
    double cnt;//覆盖一次以上的长度
    double more;//覆盖两次以上的长度
}segTree[MAXN*3];
struct Line
{
    double x,y1,y2;
    double f;
}line[MAXN];

double y[MAXN];

bool cmp(Line a,Line b)
{
    return a.x<b.x;
}

void Build(int i,int l,int r)
{
    segTree[i].l=l;
    segTree[i].r=r;
    segTree[i].cnt=0;
    segTree[i].more=0;
    segTree[i].lf=y[l];
    segTree[i].rf=y[r];
    if(l+1==r)return;
    int mid=(l+r)>>1;
    Build(i<<1,l,mid);
    Build((i<<1)|1,mid,r);
}
void calen(int i)
{
    if(segTree[i].c>=2)
    {
        segTree[i].more=segTree[i].cnt=segTree[i].rf-segTree[i].lf;
        return;
    }
    else if(segTree[i].c==1)
    {
        segTree[i].cnt=segTree[i].rf-segTree[i].lf;
        if(segTree[i].l+1==segTree[i].r)segTree[i].more=0;
        else segTree[i].more=segTree[i<<1].cnt+segTree[(i<<1)|1].cnt;
    }
    else
    {
        if(segTree[i].l+1==segTree[i].r)
        {
            segTree[i].cnt=segTree[i].more=0;
        }
        else
        {
            segTree[i].cnt=segTree[i<<1].cnt+segTree[(i<<1)|1].cnt;
            segTree[i].more=segTree[i<<1].more+segTree[(i<<1)|1].more;
        }
    }
}
void update(int i,Line e)
{
    if(e.y1==segTree[i].lf&&segTree[i].rf==e.y2)
    {
        segTree[i].c+=e.f;
        calen(i);
        return;
    }
    if(e.y2<=segTree[i<<1].rf) update(i<<1,e);
    else if(e.y1>=segTree[(i<<1)|1].lf) update((i<<1)|1,e);
    else
    {
        Line temp=e;
        temp.y2=segTree[i<<1].rf;
        update(i<<1,temp);
        temp=e;
        temp.y1=segTree[(i<<1)|1].lf;
        update((i<<1)|1,temp);
    }
    calen(i);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int n;
    double x1,y1,x2,y2;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int t=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[t].x=x1;
            //这里题目描述有问题?左下角和右上角
            line[t].y1=y1;
            line[t].y2=y2;
            line[t].x=x1;
            line[t].f=1;
            y[t]=y1;
            t++;
            line[t].y1=y1;
            line[t].y2=y2;
            line[t].x=x2;
            line[t].f=-1;
            y[t]=y2;
            t++;
        }
        sort(line+1,line+t,cmp);
        sort(y+1,y+t);
        Build(1,1,t-1);
        update(1,line[1]);
        double ans=0;
        for(int i=2;i<t;i++)
        {
            ans+=segTree[1].more*(line[i].x-line[i-1].x);
            update(1,line[i]);
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}

Time limit 1000 ms
Memory limit 30000 kB
OS Linux

题目:
You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task.
The space station is made up with a number of units, called cells. All cells are sphere-shaped, but their sizes are not necessarily uniform. Each cell is fixed at its predetermined position shortly after the station is successfully put into its orbit. It is quite strange that two cells may be touching each other, or even may be overlapping. In an extreme case, a cell may be totally enclosing another one. I do not know how such arrangements are possible.

All the cells must be connected, since crew members should be able to walk from any cell to any other cell. They can walk from a cell A to another cell B, if, (1) A and B are touching each other or overlapping, (2) A and B are connected by a `corridor’, or (3) there is a cell C such that walking from A to C, and also from B to C are both possible. Note that the condition (3) should be interpreted transitively.

You are expected to design a configuration, namely, which pairs of cells are to be connected with corridors. There is some freedom in the corridor configuration. For example, if there are three cells A, B and C, not touching nor overlapping each other, at least three plans are possible in order to connect all three cells. The first is to build corridors A-B and A-C, the second B-C and B-A, the third C-A and C-B. The cost of building a corridor is proportional to its length. Therefore, you should choose a plan with the shortest total length of the corridors.

You can ignore the width of a corridor. A corridor is built between points on two cells’ surfaces. It can be made arbitrarily long, but of course the shortest one is chosen. Even if two corridors A-B and C-D intersect in space, they are not considered to form a connection path between (for example) A and C. In other words, you may consider that two corridors never intersect.
Input
The input consists of multiple data sets. Each data set is given in the following format.

n
x1 y1 z1 r1
x2 y2 z2 r2

xn yn zn rn

The first line of a data set contains an integer n, which is the number of cells. n is positive, and does not exceed 100.

The following n lines are descriptions of cells. Four values in a line are x-, y- and z-coordinates of the center, and radius (called r in the rest of the problem) of the sphere, in this order. Each value is given by a decimal fraction, with 3 digits after the decimal point. Values are separated by a space character.

Each of x, y, z and r is positive and is less than 100.0.

The end of the input is indicated by a line containing a zero.
Output
For each data set, the shortest total length of the corridors should be printed, each in a separate line. The printed values should have 3 digits after the decimal point. They may not have an error greater than 0.001.

Note that if no corridors are necessary, that is, if all the cells are connected without corridors, the shortest total length of the corridors is 0.000.
Sample Input
3
10.000 10.000 50.000 10.000
40.000 10.000 50.000 10.000
40.000 40.000 50.000 10.000
2
30.000 30.000 30.000 20.000
40.000 40.000 40.000 20.000
5
5.729 15.143 3.996 25.837
6.013 14.372 4.818 10.671
80.115 63.292 84.477 15.120
64.095 80.924 70.029 14.881
39.472 85.116 71.369 5.553
0
Sample Output
20.000
0.000
73.834

裸的最小生成树.
注意两点之间的距离为 (球心距 - 半径和); 若这个式子小于0,则距离为0.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 110
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

struct node{
    int left,right;
    double cost;
}road[maxn*maxn];

int cmp(node x,node y) {return x.cost<y.cost;}
int p[maxn],m,n;
int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}
double kruskal()
{
    double ans=0;
    for(int i=1;i<=n;i++) p[i]=i;
    sort(road+1,road+m+1,cmp);
    for(int i=1;i<=m;i++)
    {
        int x=find(road[i].left);
        int y=find(road[i].right);
        if(x!=y)
        {
            ans+=road[i].cost;
            p[x]=y;
        }
    }
    return ans;
}

int sign(double x) {
    if(fabs(x)<eps) return 0;
    return x<0? -1:1;
}

double x[maxn],y[maxn],z[maxn],r[maxn];

double get_dis(int a, int b) {
    double d = sqrt((x[a]-x[b])*(x[a]-x[b]) + (y[a]-y[b])*(y[a]-y[b]) + (z[a]-z[b])*(z[a]-z[b]));
    if(sign(d-r[a]-r[b]) >= 0) return d-r[a]-r[b];
    return 0;
}

int main(int argc, char const *argv[])
{
    //IN;

    while(scanf("%d", &n) != EOF && n)
    {
        m = 0;
        memset(road,0,sizeof(road));

        for(int i=1; i<=n; i++) {
            scanf("%lf %lf %lf %lf", &x[i],&y[i],&z[i],&r[i]);
        }

        for(int i=1; i<=n; i++) {
            for(int j=i+1; j<=n; j++) {
                road[++m].left = i;
                road[m].right = j;
                road[m].cost = get_dis(i,j);
            }
        }

        double ans=kruskal();

        printf("%.3lf\n", ans);
    }

    return 0;
}

Time limit 2000 ms
Memory limit 65536 kB
OS Linux

题目:
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1…N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2… M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 1010
int map[maxn][maxn],n;
int way[maxn],dis[maxn];
 
void dijkstra(int x)
{
	int visit[maxn],i,j,min,next=x;
	memset(visit,0,sizeof(visit));
	for(i=1;i<=n;++i)
		dis[i]=map[x][i];
	visit[x]=1;
	for(i=2;i<=n;++i)
	{
		min=INF;
		for(j=1;j<=n;++j)
		{
			if(!visit[j]&&dis[j]<min)
			{
				min=dis[j];
				next=j;
			}
		}
		visit[next]=1;
		for(j=1;j<=n;++j)
		{
			if(!visit[j]&&dis[j]>dis[next]+map[next][j])
				dis[j]=dis[next]+map[next][j];
		}
	}
}
 
int main()
{
	int m,x,i,j,a,b,t;
	while(scanf("%d%d%d",&n,&m,&x)!=EOF)
	{
		for(i=1;i<=n;++i)
		{
			for(j=1;j<=n;++j)
			{
				if(i!=j)
					map[i][j]=INF;
				else
					map[i][j]=0;
			}
		}
		while(m--)
		{
			scanf("%d%d%d",&a,&b,&t);
			if(t<map[a][b])
				map[a][b]=t;
		}
		dijkstra(x);
		for(i=1;i<=n;++i)
			way[i]=dis[i];
		int ans=0;
		for(i=1;i<=n;++i)//置换矩阵的值,也就是交换map[i][j]与map[j][i]的值 
		{
			for(j=i+1;j<=n;++j)
			{
				int cnt;
				cnt=map[j][i];
				map[j][i]=map[i][j];
				map[i][j]=cnt;
			}
		}
		dijkstra(x);
		for(i=1;i<=n;++i)
		{
			if(i!=x)
				ans=max(ans,way[i]+dis[i]);
		} 
		printf("%d\n",ans);
	}
	return 0;
} 

Time limit 1000 ms
Memory limit 30000 kB
OS Linux

题目:
Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real R AB, C AB, R BA and C BA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.
Input
The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10 3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 10 4.
Output
If Nick can increase his wealth, output YES, in other case output NO to the output file.
Sample Input
3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00
Sample Output
YES

Bellman-Ford算法描述:
  1,.初始化:将除源点外的所有顶点的最短距离估计值 d[v] ←+∞, d[s] ←0;
  2.迭代求解:反复对边集E中的每条边进行松弛操作,使得顶点集V中的每个顶点v的最短距离估计值逐步逼近其最短距离;(运行|v|-1次)
  3.检验负权回路:判断边集E中的每一条边的两个端点是否收敛。如果存在未收敛的顶点,则算法返回false,表明问题无解;否则算法返回true,并且从源点可达的顶点v的最短距离保存在 d[v]中。

描述性证明:   首先指出,图的任意一条最短路径既不能包含负权回路,也不会包含正权回路,因此它最多包含|v|-1条边。   其次,从源点s可达的所有顶点如果 存在最短路径,则这些最短路径构成一个以s为根的最短路径树。Bellman-Ford算法的迭代松弛操作,实际上就是按顶点距离s的层次,逐层生成这棵最短路径树的过程。

代码:

#include<iostream>
using namespace std;
 
#define eps 1e-9
#define MAX_N  105
 
struct edge
{
	int u;
	int v;
	double rate;
	double cost;
}edge[2*MAX_N];
 
int N,M,s,acrNum;
double V;
 
bool Bellman_Ford()
{
	bool flag;	//判断能否收敛
	int i;
	double d[MAX_N];
	for(i=1;i<=N;i++)
		d[i] = 0.0;
	d[s] = V;
	while(d[s] <= V+eps)
	{
		flag = true;
		for(i=0;i<acrNum;i++)
		{
			double tmp = (d[edge[i].u] - edge[i].cost)*edge[i].rate;
			if(d[edge[i].v] + eps < tmp)
			{
				flag = false;
				d[edge[i].v] = tmp;
			}
		}
		if(flag)	//没有收敛,则是无法找到
			return (d[s]-V) > 0;
	}
	return 1;
}
 
int main()
{
	while(scanf("%d%d%d%lf",&N,&M,&s,&V)!=EOF)
	{
		int A,B;
		double Rab,Cab,Rba,Cba;
		for(int i=0;i<M;i++)
		{
			scanf("%d%d%lf%lf%lf%lf",&A,&B,&Rab,&Cab,&Rba,&Cba);
			//A->B
			edge[i].u = A;		edge[i].v = B;
			edge[i].rate = Rab;	edge[i].cost = Cab;
			//B->A
			edge[i+M].u = B;	edge[i+M].v = A;
			edge[i+M].rate = Rba;	edge[i+M].cost = Cba;
		}
		acrNum = 2*M;
    	if(Bellman_Ford())	printf("YES\n");
		else				printf("NO\n");
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43967023/article/details/86685501