邮递食物

来源:互联网 发布:什么是农村淘宝五个一 编辑:程序博客网 时间:2024/04/25 04:16
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 33int 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
原创粉丝点击