Train Seats Reservation

来源:互联网 发布:高仿mcm怎么在淘宝买 编辑:程序博客网 时间:2024/05/01 08:28

You are given a list of train stations, say from the station 11 to the station 100100.

The passengers can order several tickets from one station to another before the train leaves the station one. We will issue one train from the station 11 to the station 100100 after all reservations have been made. Write a program to determine the minimum number of seats required for all passengers so that all reservations are satisfied without any conflict.

Note that one single seat can be used by several passengers as long as there are no conflicts between them. For example, a passenger from station 11 to station 1010 can share a seat with another passenger from station 3030 to 6060.

Input Format

Several sets of ticket reservations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of orders, nn, which can be as large as 10001000. After nn, there will be nn lines representing the nnreservations; each line contains three integers s, t, ks,t,k, which means that the reservation needs kk seats from the station ss to the station tt .These ticket reservations occur repetitively in the input as the pattern described above. An integer n = 0n=0 (zero) signifies the end of input.

Output Format

For each set of ticket reservations appeared in the input, calculate the minimum number of seats required so that all reservations are satisfied without conflicts. Output a single star '*' to signify the end of outputs.

样例输入

21 10 820 50 2032 30 520 80 2040 90 400

样例输出

2060*

思路:

按左边的站排序,暴力枚举比较。

代码:


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;struct poin{    int l,r,v;}a[1005];int b[100];bool cmp(poin a,poin b){    if(a.l<b.l)return 1;    else if(a.l==b.l&&a.r<b.r)return 1;    else return 0;}int main(){   int n;   while(cin>>n)   {   if(n==0)break;   memset(b,0,sizeof(b));       for(int i=1;i<=n;i++)       {         int q,w,e;         scanf("%d%d%d",&q,&w,&e);         a[i].l=q;a[i].r=w;a[i].v=e;       }       sort(a+1,a+n+1,cmp);       int sum=a[1].v;       for(int i=2;i<=n;i++)       {           memset(b,0,sizeof(b));           for(int j=1;j<i;j++)           {              if(a[i].l<a[j].r){sum=max(sum,a[i].v+a[j].v);b[j]=1;}           else {sum=max(sum,a[i].v);}           }           int su=a[i].v;              for(int j=1;j<i;j++)           {             if(b[j])su+=a[j].v;           }           sum=max(su,sum);       }     printf("%d\n",sum);   }   printf("*");    return 0;}





阅读全文
0 0
原创粉丝点击