首页 > 其他分享 >PAT (Advanced Level) Practice 1003 Emergency 分数 25

PAT (Advanced Level) Practice 1003 Emergency 分数 25

时间:2022-09-23 19:22:05浏览次数:48  
标签:distance PAT Emergency 25 int num dist c1 city

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1​ and C2​ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1​, c2​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1​ to C2​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1​ and C2​, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
 

Sample Output:

2 4
  代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB

Part 1

#include<stdio.h>

typedef struct
{

int distance;
}Dist;

typedef struct
{
int num;
Dist dist[501];
}cityinf;

int main()
{
int i,j,l,n,m,c1,c2;
int shortdist,sumdist=0;
int maxnum;
cityinf city[501];

scanf("%d %d %d %d\n",&n,&m,&c1,&c2);
for(i=0;i<n;i++)
{
scanf("%d ",&city[i].num);
}
for(i=0;i<m;i++)
{
scanf("%d ",&j);
scanf("%d ",&l);
scanf("%d\n",&city[j].dist[l].distance);
}
//printf("%d\n%d",city[3].num,city[3].dist[4].distance);
/*if(city[c1].dist[c2].distance)
{
shortdist=city[c1].dist[c2].distance;
maxnum=city[c1].num+city[c2].num;
}
for(i=0;i<n;i++)
{
if(i!=c1&&city[i].dist[c2].distance>0)
{
sumdist+=city[i].dist[c2].distance;
for(j=0;j<n;j++)
{
if(city[j].dist[i].distance>0)
{
sumdist+=city[j].dist[i].distance;
if(j!=c1)
{
i=j;
}
if(j==c1)
{
if(sumdist<=shortdist) shortdist=sumdist;
//sumdist+=city[c1].dist[j].distance;
sumdist=0;
break;
}
}
else
continue;
}
//if(sumdist<=shortdist) shortdist=sumdist;
}
}*/
printf("%d %d",shortdist,maxnum);

 

Part 2

#include<stdio.h>

typedef struct
{

int distance;
}Dist;

typedef struct
{
int num;
Dist dist[501];
}cityinf;

int main()
{
int i,j,l,n,m,c1,c2,tempc;
int shortdist=1000000,sumdist=0;
int maxnum=0,sumnum=0;
cityinf city[501];

scanf("%d %d %d %d\n",&n,&m,&c1,&c2);
for(i=0;i<n;i++)
{
scanf("%d ",&city[i].num);
}
for(i=0;i<m;i++)
{
scanf("%d ",&j);
scanf("%d ",&l);
scanf("%d\n",&city[j].dist[l].distance);
}
tempc=c1;
for(i=0;i<n;i++)
{
c1=tempc;
sumdist=0;
sumnum=city[c1].num;
for(j=i;j<n;j++){
if(city[c1].dist[j].distance>0&&j!=c2)
{
sumnum+=city[j].num;
sumdist+=city[c1].dist[j].distance;
c1=j;
//printf("test1 %d\n",c1);
}
if(city[c1].dist[j].distance>0&&j==c2)
{
if(maxnum<sumnum+city[j].num)
{
maxnum=sumnum+city[j].num;
//printf("test2\n");
}
if(shortdist>sumdist+city[c1].dist[j].distance)
{
shortdist=sumdist+city[c1].dist[j].distance;
//printf("test3\n");
}
//printf("test4\n");
break;
}
/*if(city[c1].dist[j].distance==0)
{
shortdist=0;
maxnum=city[c1].num;
printf("test5\n");
}*/
}
}
if(c1==c2)
{
printf("0 %d",city[c1].num);
}
else printf("%d %d",shortdist,maxnum);
}

 

 

#include<stdio.h>

typedef struct
{
int distance;
}Dist;

typedef struct
{
int num;
Dist dist[501];
}cityinf;

int main()
{
int i,j,l,v,u,n,m,c1,c2;
int shortdist;
int num[501];
int w[501];
int dis[501];
cityinf city[501];
bool visit[501] = {0};
for(i=0;i<501;i++)
{
dis[i]=1000000;
for(j=0;j<501;j++)
{
city[i].dist[j].distance=1000000;
}
}
scanf("%d %d %d %d\n",&n,&m,&c1,&c2);
for(i=0;i<n;i++)
{
scanf("%d ",&city[i].num);
}
for(i=0;i<m;i++)
{
scanf("%d ",&j);
scanf("%d ",&l);
scanf("%d\n",&city[j].dist[l].distance);
city[l].dist[j].distance=city[j].dist[l].distance;
}
dis[c1]=0;
w[c1]=city[c1].num;
num[c1]=1;
for(i=0;i<n;i++)
{
u = -1;
shortdist=1000000;
for(j=0;j<n;j++)
{
if(visit[j] == false && dis[j]<shortdist)
{
u=j;
shortdist=dis[j];
}
}
if(u == -1) break;
visit[u] = true;
for(v=0;v<n;v++)
{
if(visit[v]==false&&city[u].dist[v].distance!= 1000000)
{
if(dis[u]+city[u].dist[v].distance<dis[v])
{
dis[v]=dis[u]+city[u].dist[v].distance;
num[v] = num[u];
w[v]=w[u]+city[v].num;
}else if(dis[u]+city[u].dist[v].distance==dis[v])
{
num[v] =num[v] + num[u];
if(w[u]+city[v].num>w[v])
{
w[v]=w[u]+city[v].num;
}
}
}
}
}
printf("%d %d",num[c2],w[c2]);
return 0;
}

标签:distance,PAT,Emergency,25,int,num,dist,c1,city
From: https://www.cnblogs.com/slowlydance2me/p/slowlydance2me_1003A.html

相关文章