1064 - Network

本文深入探讨了包交换网络中信息处理的关键技术,特别是如何通过最小缓冲区大小来实现消息的正确重组。通过实例解析,文章详细阐述了消息分解、数据包路径选择、接收计算机的内存缓冲区管理以及最终消息重构的过程。重点讨论了如何在不同数据包到达时间和顺序不一致的情况下,确保原始消息的完整性和正确性。

A packet-switching network handles information in small units, breaking long messages into multiple packets before routing. Although each packet may travel along a different path, and the packets comprising a message may arrive at different times or out of order, the receiving computer reassembles the original message correctly.

The receiving computer uses a buffer memory to hold packets that arrive out of order. You must write a program that calculates the minimum buffer size in bytes needed to reassemble the incoming messages when the number of messages (N ), the number of packets (M ), the part of the messages in each packet, the size of each message, and the order of the incoming packets are given.

When each packet arrives, it may be placed into the buffer or moved directly to the output area. All packets that are held in the buffer are available to be moved to the output area at any time. A packet is said to ``pass the buffer" when it is moved to the output area. A message is said to ``pass the buffer" when all of its packets have passed the buffer.

The packets of any message must be ordered so the data in the sequence of packets that pass the buffer is in order. For example, the packet containing bytes 3 through 5 of a message must pass the buffer before the packet containing bytes 6 through 10 of the same message. Messages can pass the buffer in any order, but all packets from a single message must pass the buffer consecutively and in order (but not necessarily at the same time). Note that unlike actual buffering systems, the process for this problem can look ahead at all incoming packets to make its decisions.

The packets consist of data and header. The header contains three numbers: the message number, the starting byte number of data in the packet, and the ending byte number of data in the packet respectively. The first byte number in any message is 1.

For example, the figure below shows three messages (with sizes of 10, 20, and 5 bytes) and five packets. The minimum buffer size for this example is 10 bytes. As they arrive, packet #1 and packet #2 are stored in the buffer. They occupy 10 bytes. Then packet #3 passes the buffer directly. Packet #4 passes the buffer directly and then packet #2 exits the buffer. Finally, packet #5 passes the buffer directly and packet #1 exits the buffer.

\epsfbox{p3808.eps}

Input 

The input file contains several test cases. The first line of each test case contains two integers N (1$ \le$N$ \le$5) and M (1$ \le$M$ \le$1000) . The second line contains N integers that are the sizes of messages in bytes; the first number denotes the size of message #1, the second number denotes the size of message #2, and so on. Each of the following M lines describes a packet with three integers: the message number and the starting and ending byte numbers of data in the packet. No packet contains more 64 bytes of data.

The last test case is followed by a line containing two zeroes.

Output 

For each test case, print a line containing the test case number (beginning with 1) followed by the minimum buffer size in bytes required to reassemble the original messages. Put a blank line after the output for each test case. Use the format of the sample output.

Sample Input 

3 3 
5 5 5 
1 1 5 
2 1 5 
3 1 5 
3 5 
10 20 5 
2 16 20 
1 6 10 
3 1 5 
1 1 5 
2 1 15 
0 0

Sample Output 

Case 1: 0 

Case 2: 10




#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char data[5][64200];
int n,m,cases,i,ans,size[5],selected[5],packet[1009],start[1009],end[1009],queue[5];

void work()
{
	int i,cur,head,len,maxl,used[5];
	memset(data,0,sizeof(data));
	memset(used,0,sizeof(used));
	cur=0;
	head=0;
	maxl=0;
	for(i=0;i<m;i++)
	{
		len=end[i]-start[i]+1;
		used[packet[i]]+=len;
		memset(&data[packet[i]][start[i]],1,len);
		while(data[queue[cur]][head+1]&&head<size[queue[cur]])
		{
			head++;
			used[queue[cur]]--;
		}
		if(head>=size[queue[cur]])
		{
			cur++;
			head=0;
		}
		len=used[0]+used[1]+used[2]+used[3]+used[4];
		if(len>maxl)
			maxl=len;
	}
	if(maxl<ans)
		ans=maxl;
}

void go(int i)
{
	int j;
	if(i>=n)
		work();
	for(j=0;j<n;j++)
		if(!selected[j])
		{
			selected[j]=1;
			queue[i]=j;
			go(i+1);
			selected[j]=0;
		}
}

int main()
{
	while(scanf("%d%d",&n,&m)&&n)
	{
		for(i=0;i<n;i++)
			scanf("%d",&size[i]);
		for(i=0;i<m;i++)
		{
			scanf("%d%d%d",&packet[i],&start[i],&end[i]);
			packet[i]--;
		}
		ans=1<<30;
		go(0);
		printf("Case %d: %d\n\n",++cases,ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不仅仅是寻找

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值