邮递食物

10 篇文章 0 订阅
Problem Description
College students have wide freedom in many circumstances. For instance, nowadays, more and more students choose to order food from outside school, instead of eating in the school canteens. Owing to the rapid development of network communication, the ways students ordering food are multifarious. They can order food on the phone, as well as through network such as QQ or BBS. Ordering food is convenient and laborsaving, however, sometimes many students may complain about waiting too long time for the food and some students may even eat other food instead because of not being able to wait so long. As a result, the students who receive food late will be depressed and the benefit and credit of the fast food canteens will be debased.
Ivan and Cristy graduated from the same college recently. Considering the awful circumstances above, they establish a delivery company (named IC), which controls the food delivery of the fast food canteens. They hope the IC Company provides students with good and fast services in scientific, rational and concentrative ways of delivery. Certainly, they hope to benefit at the same time.
Now, let’s suppose there are N places, one of which is the unique food delivery center and all the food are sent from it. The other N-1 places are dorms to which food must be delivered. Each dorm has a time limit after which food can not be accepted, that is to say, the food ordered by the students in a dorm must be received before the dorm’s time limit. Because the IC Company adopts scientific and concentrative methods, all the food needed are well prepared and there is no limit in the amount of food that can be delivered at a time. However, because the company has been set up only for a short time, there is only one delivery team at work for the moment. In addition, in order to estimate the efficiency of delivery, the IC Company will compute the time for delivery. They set the time when the food is just sent from the delivery center 0, and the distance between two places is denoted as the walking time between them. Because there may be more than one way connecting two places, the time need to walk from one place to another place may be not unique.
Now, your job is to help the IC Company to find out a delivery way, which will satisfy all the requests of the dorms (arrive at every dorm before the dorm’s time limit) and minimize the total waiting time of all the dorms (the waiting time of a dorm is from 0 to the time the food is accepted by the students in the dorm). If there is no way satisfying all the requests of the dorms, the IC Company will regard the delivery as a failure.
Input
Input will contain several test cases. Each test case begins with a line containing an integer n (2≤n≤30), representing the number of places, and we suppose the first place is the delivery center. In the following n lines, each line represents one place and contains n positive integers (include 0) separated by a single space. The jth integer of the ith line is the walking time from the ith place to the jth place. The next line after the n lines contains n-1 positive integer, representing the time limits of the dorms.
The last test case is followed by a line containing one zero. No extra spaces at the beginning/end of each line.
Output
For each test case in the input you should output an integer, representing the minimum total waiting time of all the dorms. If no solution is found, you should output -1 in the corresponding line.
Sample Input
4
0 3 8 6
4 0 7 4
7 5 0 2
6 9 3 0
30 8 30
3
0 10 10
10 0 1
10 1 0
10 10
0
Sample Output
36

-1

/*#include<stdio.h>
#include<string.h>

int n;     //顶点数
int map[50][50];  //邻接矩阵
int tl[50];       //时间限制
int best;         //最佳访问时间
int visit[50];    //访问时间

bool read_data()
{
	scanf("%ld",&n);
	if(n==0) return false;

	int i,j;
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			scanf("%ld",&map[i][j]);
	for(i=2;i<=n;i++) scanf("%ld",&tl[i]);
	return true;
}

void find_shortest()   //利用Floyd算法求任意两点之间的距离
{
	int i,j,k;
	for(k=1;k<=n;k++)
		for(i=1;i<=n;i++)
			if(i!=k)
				for(j=1;j<=n;j++)
					if(i!=j&&j!=k)
						if(map[i][k]+map[k][j]<map[i][j]) 
							map[i][j]=map[i][k]+map[k][j];
}

void ready()  //数据初始化
{
	find_shortest();
	best=-1;
	memset(visit,0,sizeof(visit));
}

bool no_solution()  //预处理判断是否有解
{
	int i,j;
	int x=map[1][2];
	for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
			if(i!=j&&map[i][j]<x) //寻找最长路径
				x=map[i][j];
	for(i=2;i<=n;i++)
		if(tl[i]>=x*(n-1)) return false;  //最长路径比时间要短,有解

	return true;   //不符合最长路径比时间短,无解
}

bool ontime(int x,int t)  //优化函数,当前直接到达某个顶点是否及时
{
	int i;
	for(i=2;i<=n;i++)
		if(visit[i]==0&&t+map[x][i]>tl[i])  //当前未访问不能及时到达,剪掉
			return false;
	return true;
}

void search(int p, int x, int t) //深度优先搜索,其中p为已搜索顶点数,t为总时间,x为当前顶点
{
	if(p>n)   //已经找到一种方案
	{
		if(t<best||best==-1) best=t;  //修改最优方案
		return;
	}
	if(!ontime(x,visit[x])) return;  //利用优化判断当前是否能到
	if(t+visit[x]*(n-p+1)>=best&&best>-1) return;  //当前搜索不可能比之前的更优

	int i;
	for(i=2;i<=n;i++)  //深度优先搜索
		if(visit[i]==0)
		{
			visit[i]=visit[x]+map[x][i];
			search(p+1,i,t+visit[i]);
			visit[i]=0;
		}
}


int main()
{
	while(read_data())
	{
		ready();
		if(!no_solution())
			search(2,1,0);
		printf("%ld\n",best);
	}
	return 0;
}*/
#include<stdio.h>
#include<string.h>
#define INF 1<<23
#define Max 33
int map[Max][Max],n,dis[Max],vis[Max];
int tmp[Max],minn,flag;
void find_shortest()   //利用Floyd算法求任意两点之间的距离
{
	int i,j,k;
	for(k=0;k<n;k++)
		for(i=0;i<n;i++)
			if(i!=k)
				for(j=0;j<n;j++)
					if(i!=j&&j!=k)
						if(map[i][k]+map[k][j]<map[i][j]) 
							map[i][j]=map[i][k]+map[k][j];
}
bool no_solution()  //预处理判断是否有解
{
    int i,j;
    int x=map[0][1];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            if(i!=j&&map[i][j]<x) //寻找最长路径
                x=map[i][j];
    for(i=1;i<n;i++)
        if(dis[i]>=x*(n-1)) return false;  //最长路径比时间要短,有解

    return true;   //不符合最长路径比时间短,无解
}
bool ontime(int last,int sum,int tot)
{
	int i;
	for(i=0;i<n;i++)
		if(!vis[i]&&sum+map[last][i]>dis[i]) return 0;
		return 1;
}
void dfs(int last,int cnt,int sum,int tot)
{
	int i;
	//if(tot>minn) return;
	if(!ontime(last,sum,tot)) return;
	//if(t+visit[x]*(n-p+1)>=best&&best>-1) return; 
	if(tot+sum*(n-cnt-1)>=minn) return;
	if(cnt>=n-1)
	{
		flag=1;
		if(tot<minn)
			minn=tot;
		return;
	}
	for(i=0;i<n;i++)
	{
		if(!vis[i])
		{
			vis[i]=sum+map[last][i];
			dfs(i,cnt+1,vis[i],tot+vis[i]);
			vis[i]=0;
		}
	}
}
int main()
{
	freopen("b.txt","r",stdin);
	while(scanf("%d",&n)==1&&n)
	{
		int i,j;
		for(i=0;i<n;i++)
		{
			for(j=0;j<n;j++)
				scanf("%Id",&map[i][j]);
		}
		dis[0]=0;
		for(j=1;j<n;j++)
			scanf("%d",&dis[j]);
		memset(vis,0,sizeof(vis));
		find_shortest();
		vis[0]=1;
		minn=INF;
		flag=0;
		if(no_solution()) {printf("-1\n");continue;}
		dfs(0,0,0,0);
		if(flag)
			printf("%d\n",minn);
		else printf("-1\n");
	}
	return 0;
}

注释的为AC代码,没注释的为本人写的代码,WA。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邮递员问题是一个著名的旅行商问题。该问题的目标是到一条路线,使得一个邮递员可以在一个城市中经过所有的街道,然后返回起点,而且路线的总长度最短。 C++代码如下: ```c++ #include <iostream> #include <algorithm> #include <cstdlib> #include <ctime> using namespace std; const int N = 5; const int INF = 1000000; // 表示两点之间没有连通 int graph[N][N] = { {0, 2, 6, 5, INF}, {2, 0, INF, 7, 1}, {6, INF, 0, INF, 3}, {5, 7, INF, 0, INF}, {INF, 1, 3, INF, 0}, }; // 城市地图 void print(int *path) { // 打印路径 for (int i = 0; i < N; ++i) { cout << path[i] << " "; } cout << path[0] << endl; // 回到起点 } void solve() { int path[N]; for (int i = 0; i < N; ++i) { path[i] = i; } int ans = INF; do { int sum = 0; for (int i = 0; i < N - 1; ++i) { if (graph[path[i]][path[i+1]] == INF) { sum = INF; break; } sum += graph[path[i]][path[i+1]]; } if (graph[path[N-1]][path[0]] != INF) { sum += graph[path[N-1]][path[0]]; // 回到起点 } else { sum = INF; } if (sum < ans) { ans = sum; print(path); } } while (next_permutation(path, path + N)); cout << "最短路线长度为:" << ans << endl; } int main() { srand(time(NULL)); solve(); return 0; } ``` 该代码中,我们通过 `graph` 数组存储了城市地图,其中 `INF` 表示两点之间没有连通。`solve` 函数通过枚举所有可能的路径,并计算路径长度,来到最短路径。在计算路径长度时,如果某个点与下一个点之间没有连通,则路径长度为 INF。最后我们通过 `print` 函数打印路径,通过 `ans` 变量保存最短路径长度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值