这个比较难
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了这个比较难相关的知识,希望对你有一定的参考价值。
[最短路径问题]这是一幅美国硅谷的简单地图。对于此图,请写出一个完整的程序,对于输入的起点和终点,输出它们之间的最短路径。
最短路径是经过距离最短
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INFINITY 1000
#define MAX 20
typedef struct
int vexnum;
int arcs[MAX][MAX];
MGraph;
void ShortestPath_DIJ(MGraph G,int v0,int P[MAX][MAX], int D[MAX]);
void main ()
int i,j,k,v0,arcsnum;
int P[MAX][MAX],D[MAX];
MGraph G;
printf ("输入顶点数n=\n");
scanf ("%d",&G.vexnum);
printf ("输入弧度数arcsnum=\n");
scanf ("%d",&arcsnum);
printf ("输入开始访问顶点v0(小于%d)\n",G.vexnum);
scanf ("%d",&v0);
for (i=0;i<G.vexnum;i++)
for (j=0;j<G.vexnum;j++)
G.arcs[i][j]=INFINITY;
for (i=0;i<arcsnum;i++)
printf ("输入有权值的弧的头尾下标:arcs[j][k]=\n");
scanf ("%d %d",&j,&k);
printf ("输入权值 G.arcs[%d][%d]=\n",j,k);
scanf ("%d",&G.arcs[j][k]);
printf ("输入的矩阵为:\n");
for (i=0;i<G.vexnum;i++)
for (j=0;j<G.vexnum;j++)
if (G.arcs[i][j]==INFINITY)
printf ("∞\t");
else printf ("%d\t",G.arcs[i][j]);
printf ("\n");
ShortestPath_DIJ(G,v0,P,D);
printf ("\n-----------输出结果------------\n");
for (i=0;i<G.vexnum;i++)
if (i!=v0)
if (D[i]==INFINITY)
printf ("from %d to %d is 无路径",v0,i);
else
printf ("from %d to %d is-->>%d\n",v0,i,D[i]);
printf ("所经路径为:%d",v0);
for (k=1;k<=G.vexnum+2;k++)
for (j=1;j<G.vexnum;j++)
if (P[i][j]==k && j!=i)
printf ("-->>%d",j);
if (D[i]<INFINITY)
printf ("-->>%d\n",i);
else
printf ("\n");
printf ("\n");
void ShortestPath_DIJ(MGraph G,int v0,int P[MAX][MAX], int D[MAX])
//用Dijkstra算法求有向网G的v0顶点到其余顶点v的最短路径//P[v]及其带权长度D[v].
//若P[v][w]为TRUE,则w是从v0到v当前求得最短路径上的顶点
//final[v]为TRUE当且仅当v∈S,即已经求得从v0到v的最短路//径.
int final[MAX],v,w,i,j,k,sum[MAX],min;
for (k=0;k<G.vexnum;k++)
sum[k]=0;
for(v=0;v<G.vexnum;++v)
final[v]=0;
D[v]=G.arcs[v0][v];
for(w=0;w<G.vexnum;++w)
P[v][w]=0;//设空路径
if(D[v]<INFINITY)
P[v][v0]=1;
P[v][v]=1;
//for
D[v0]=0;
final[v0]=1; //初始化,v0顶点属于S集
//开始主循环,每次求得v0到某个v顶点的最短路径,并加v到S集
for(i=1;i<G.vexnum;++i)
//其余G.vexnum-1各顶点
min=INFINITY; //当前所知离v0顶点的最近距离
for(w=0;w<G.vexnum;++w)
if(!final[w]) //w顶点在V-S中
if(D[w]<min)
v=w;min=D[w];
//w顶点离v0更近
final[v]=1; //离v0顶点最近的v加入S集
for(w=0;w<G.vexnum;++w)
//更新当前最短路径及距离
if(!final[w] && (min+G.arcs[v][w]<D[w]))
//修改D[w]和P[w],w∈V-S
D[w]=min+ G.arcs[v][w];
for(j=0;j<G.vexnum;j++)
P[w][j]=P[v][j];
for (k=0;k<G.vexnum;k++)
sum[i]+=P[i][k];
P[w][w]=sum[i]+1;
//if
参考技术A 用最短路径算法,迪杰斯特拉算法,网上很多,你给它初始化一下就OK了 参考技术B 你是说最短路径是经过地点最少还是距离最短? 参考技术C 你懂得图的各种操作就不难了~
PAT 1026 Table Tennis[比较难]
1026 Table Tennis (30)(30 分)
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For any pair of players, if there are some tables open when they arrive, they will be assigned to the available table with the smallest number. If all the tables are occupied, they will have to wait in a queue. It is assumed that every pair of players can play for at most 2 hours.
Your job is to count for everyone in queue their waiting time, and for each table the number of players it has served for the day.
One thing that makes this procedure a bit complicated is that the club reserves some tables for their VIP members. When a VIP table is open, the first VIP pair in the queue will have the priviledge to take it. However, if there is no VIP in the queue, the next pair of players can take it. On the other hand, if when it is the turn of a VIP pair, yet no VIP table is available, they can be assigned as any ordinary players.
Input Specification:
Each input file contains one test case. For each case, the first line contains an integer N (<=10000) - the total number of pairs of players. Then N lines follow, each contains 2 times and a VIP tag: HH:MM:SS - the arriving time, P - the playing time in minutes of a pair of players, and tag - which is 1 if they hold a VIP card, or 0 if not. It is guaranteed that the arriving time is between 08:00:00 and 21:00:00 while the club is open. It is assumed that no two customers arrives at the same time. Following the players‘ info, there are 2 positive integers: K (<=100) - the number of tables, and M (< K) - the number of VIP tables. The last line contains M table numbers.
Output Specification:
For each test case, first print the arriving time, serving time and the waiting time for each pair of players in the format shown by the sample. Then print in a line the number of players served by each table. Notice that the output must be listed in chronological order of the serving time. The waiting time must be rounded up to an integer minute(s). If one cannot get a table before the closing time, their information must NOT be printed.
Sample Input:
9
20:52:00 10 0
08:00:00 20 0
08:02:00 30 0
20:51:00 10 0
08:10:00 5 0
08:12:00 10 1
20:50:00 10 0
08:01:30 15 1
20:53:00 10 1
3 1
2
Sample Output:
08:00:00 08:00:00 0
08:01:30 08:01:30 0
08:02:00 08:02:00 0
08:12:00 08:16:30 5
08:10:00 08:20:00 10
20:50:00 20:50:00 0
20:51:00 20:51:00 0
20:52:00 20:52:00 0
3 3 2
我的代码:
#include <stdio.h> #include<iostream> #include <algorithm> using namespace std; struct Play{ int arrive,serve,ing,ed,vip; int waiting; Play(){ serve=-1;//表示没被服务。 } }play[10000]; struct Table{ int vip,people; int ing;//是否正在使用 Table(){ vip=0;ing=-1;people=0; } }table[101]; bool cmp(Play& a,Play& b){ return a.arrive<b.arrive;//从小到大。 } bool cmp2(Play& a,Play& b){ return a.serve<b.serve; } int main() { int n,m,vm,vmno; scanf("%d",&n); int h,miu,s,bg=8*3600; for(int i=0;i<n;i++){ scanf("%d:%d:%d",&h,&miu,&s); play[i].arrive=h*3600+miu*60+s-bg; scanf("%d %d",&play[i].ing,&play[i].vip); play[i].ing*=60; if(play[i].ing>7200) play[i].ing=7200; } sort(play,play+n,cmp); scanf("%d%d",&m,&vm); for(int i=1;i<=vm;i++){ scanf("%d",&vmno); table[vmno].vip=1;//是vip桌子。 } // for(int i=0;i<n;i++){ // printf("%02d:%02d:%02d ",play[i].arrive/3600+8,play[i].arrive%3600/60,play[i].arrive%60); // printf("%d %d ",play[i].ing,play[i].vip); // //printf("%02d:%02d:%02d ",play[i].serve/3600+8,play[i].serve%3600/60,play[i].serve%60); // } //一定要注意要服务一个人的时候,需要保证那个人已经到了。。。 //注意,题目要求,如果当前队列中没有VIP选手,那么正常选手可以使用VIP桌子。 int ct=0; for(int tm=0;tm<46800;tm++){ //判断当前是否有结束的 //不能用来计数,因为有可能serve到。 if(ct==n-1)break;//人已经分配完了。 for(int i=1;i<=m;i++){ if(table[i].ing!=-1&&play[table[i].ing].ed==tm){//判断当前服务是否结束 table[i].ing=-1; } } int bg=0,j,k;//记录开始循环的地方。 for(int i=1;i<=m;i++){ j=bg; if(table[i].ing==-1&&table[i].vip==1){//分配空闲vip桌子,分配vip用户。 for(j=bg;j<n;j++){ if(play[j].arrive<=tm&&play[j].vip==1&&play[j].serve==-1){//当前选手是vip,且没被服务 table[i].ing=j; table[i].people++; play[j].serve=tm; play[j].ed=tm+play[j].ing;ct++; // printf("%d %d %d %d vip ",i,j,tm,tm-play[j].arrive); break;//跳出内层循环 } } bg=j; } } bg=0; for(int i=1;i<=m;i++){ k=bg; if(table[i].ing==-1){//分配桌子与群众 for(k=bg;k<n;k++){ if(play[k].arrive<=tm&&play[k].serve==-1){ table[i].ing=k; table[i].people++; play[k].serve=tm; play[k].ed=tm+play[k].ing;ct++; // printf("%d %d %d %d ",i,j,tm,tm-play[j].arrive); break; } } bg=k;//加了这个之后就可以了,不用每次都从头开始遍历。有一个标记。 } } } ct=0; sort(play,play+n,cmp2); for(int i=0;i<n;i++){ if(play[i].serve!=-1){ printf("%02d:%02d:%02d ",play[i].arrive/3600+8,play[i].arrive%3600/60,play[i].arrive%60); printf("%02d:%02d:%02d ",play[i].serve/3600+8,play[i].serve%3600/60,play[i].serve%60); if((play[i].serve-play[i].arrive)%60>=30) ct=1; else ct=0; printf("%d ",(play[i].serve-play[i].arrive)%3600/60+ct); } } printf("%d",table[1].people); for(int i=2;i<=m;i++){ printf(" %d",table[i].people); } return 0; }
//!!!在牛客网上AC 了,但是在pat上就只有16分,只通过了两个点,其他的都是答案错误,真是哭唧唧,完全不知道该怎么改了。!!先放这。明天复习一下。绝望了。
其中第一次提交通过80%,有如下没通过:
测试用例:
4
08:00:00 60 0
08:10:00 50 0
08:50:00 30 0
09:00:00 30 1
2 1
2
对应输出应该为:
08:00:00 08:00:00 0
08:10:00 08:10:00 0
09:00:00 09:00:00 0
08:50:00 09:00:00 10
2 2
你的输出为:
08:00:00 08:00:00 0
08:10:00 08:10:00 0
08:50:00 09:00:00 10
改了cmp2函数之后,提交之后报:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。
看来我这么循环着做,不太对啊。复杂度太高了,一直循环。
以上是关于这个比较难的主要内容,如果未能解决你的问题,请参考以下文章