首页 > 代码库 > poj 3038

poj 3038

Flying Right
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1580 Accepted: 557

Description

Figuring that they cannot do worse than the humans have, Farmer John‘s cows have decided to start an airline. Being cows, they decide to cater to the heretofore-untapped market of cows as passengers. They plan to serve the cows who live along the western coast of Lake Michigan. Each morning, they will fly from the northern-most point of the coast southward towards Chicowgo, making many stops along the way. Each evening, they will fly back north to the northern-most point. 

They need your help to decide which passengers to carry each day. Each of N (1 <= N <= 10,000) farms numbered 1..N along the coast contains an airport (Farm 1 is northern-most; farm N is southern-most). On this day, K (1 <= K <= 50,000) groups of cows wish to travel.Each group of cows wants to fly from a particular farm to another particular farm. The airline, if it wishes, is allowed to stop and pick up only part of a group. Cows that start a flight, however,must stay on the plane until they reach their destination. 

Given the capacity C (1 <= C <= 100) of the airplane and the groups of cows that want to travel, determine the maximum number of cows that the airline can fly to their destination.

Input

* Line 1: Three space-separated integers: K, N, and C 

* Lines 2..K+1: Each line contains three space-separated integers S, E, and M that specify a group of cows that wishes to travel. The M (1 <= M <= C) cows are currently at farm S and want to travel to farm E (S != E).

Output

* Line 1: The maximum number of cows that can be flown to their destination. This is the sum of the number of cows flown to their destination on the flight southward in the morning plus the number of cows flown to their destination on the flight northward in the evening.

Sample Input

4 8 31 3 22 8 34 7 18 3 2

Sample Output

6

Hint

INPUT DETAILS: 
Four groups of cows, eight farms, and three seats on the plane. 

OUTPUT DETAILS: 
In the morning, the flight takes 2 cows from 1->3, 1 cow from 2->8,and 1 cow from 4->7. In the evening, the flight takes 2 cows from 8->3.
思想很好**注意三个问题:* 1.使用DFS计算左转优先和右转优先的路径,使用BFS计算最短路径* 2.这里的DFS不需要标记,因为按照方向顺时针(或逆时针)前进时,除非无路可走才会返回,所以不会因为没有标记而出现问题,不过它的前进方向是相对,是相对当前位置进行左上右下(右前左后)探索(这个相对性非常重要),最初的方向由起点S确定,而下一步的方向则由前一步的走向决定 以顺时针,左手优先为例,即(相对于当前方向,左转90°后的方向为第一优先,依次右转90°找可能的通路) 如何左转90度?0,1,2,3代表四个方向,显示直接-1是不行的,可能变成负值了,那么可以用(d+3)%4,就可以左转90度到优先位置,当然往右转的过程中,还是会超过0,1,2,3,到了外面,所以在运算时,使用d%4就可以了。 * 3.起点到终点顺时针的话,它的逆时针也可以使用顺时针所使用的DFS,只要把起点和终点倒过来就可以了,也就是DFS可以共用。#include <iostream>#include <cstdio>#include <cstring>#include <queue> using namespace std;const int Max = 50;int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}}; //以左上右下(顺时针)为基本方向数组char map[Max][Max];int vis[Max][Max];//广搜标记数组 int sx,sy,ex,ey;int w,h,count,flag;struct node{	int x,y;	int dis;}now,temp;void dfs(int x, int y, int tx, int ty, int d)  {	int nx,ny;	if(x == tx && y == ty)	{		flag = 1;		return;	}		d = (d+3)%4; //在上一个方向的基础上,左转90度到优先位置	int i = d; 	for(i=d; i<d+4; i++)	{		nx = x + dir[i%4][0];				ny = y + dir[i%4][1];		if(nx >= 0 && nx < h && ny >= 0 && ny < w && map[nx][ny] != ‘#‘)		{			count++;			d = i; //记录当前方向 			dfs(nx, ny, tx, ty, d);			if(flag)				return;		}	}}	void bfs(){	queue<node> q;	now.x = sx;	now.y = sy;	now.dis = 1;	q.push(now);	while(!q.empty())	{		now = q.front();		q.pop();		if(map[now.x][now.y] == ‘E‘)		{			cout<<now.dis<<endl;			return;		}			for(int i=0; i<4; i++)		{			temp.x = now.x + dir[i][0];			temp.y = now.y + dir[i][1];			temp.dis = now.dis + 1;			if(temp.x >=0 && temp.x <= h && temp.y >= 0 && temp.y < w && map[temp.x][temp.y] != ‘#‘ && !vis[temp.x][temp.y])			{				vis[temp.x][temp.y] = 1;				q.push(temp);			}		}	}	}	int main(){	int t;	while(scanf("%d",&t) != EOF)	{		while(t--)		{	 		scanf("%d %d",&w,&h);	 		for(int i=0; i<h; i++) //记住这里用h 	 		{	 			getchar(); 	 			for(int j=0; j<w; j++) //记住这里用w 	 			{		 				scanf("%c",&map[i][j]);					if(map[i][j] == ‘S‘)	 				{						 					sx = i;	 					sy = j;	 				}	 				if(map[i][j] == ‘E‘)	 				{	 					ex = i;	 					ey = j;	 				}	 			}	 		}	 			flag = 0;	 			count = 1;	 			dfs(sx, sy, ex, ey, 0); //顺时针,左手边,初始方向无所谓,因为只有1个出口,反正会调整好  	 			cout<<count<<" ";	 			flag = 0;	 			count = 1; 				dfs(ex, ey, sx, sy, 0); //逆时针(右手边)  	 			cout<<count<<" "; 	 			memset(vis, 0, sizeof(vis));  	 			bfs();		}	 }	 return 0;}

  

poj 3038