GitHub

markdownPDF


问题描述

Emergency (25)

时间限制 400 ms

内存限制 65536 kB

代码长度限制 16000 B

判题程序 Standard

作者 CHEN, Yue

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

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

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

大意是:

n个城市m条路,每个城市都有救援小组。已知各边权值,给定起点和重点,求从起点到重点的最短路径条数以及最短路径上的救援小组数目之和。

n<=500.


解题思路

最短路径,可以用dijsktra或深搜(数据不大)。dijsktra多记录一下路径数和小组总数。


代码

dijsktra

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k,n,m,s,t,c1,c2,a[501][501],b[501],c[501],d[501],e[501],f[501]={0};
memset(a,0x3f,sizeof(a));
memset(d,0x3f,sizeof(d));
cin>>n>>m>>c1>>c2;
for (i=0;i<n;i++) cin>>b[i];
for (i=0;i<m;i++)
{
cin>>j>>k>>t;
a[j][k]=a[k][j]=t;
}
c[c1]=b[c1];
d[c1]=0;
e[c1]=1;
for (i=0;i<n;i++)
{
s=0x3f3f3f3f;
t=-1;
for (j=0;j<n;j++)
if ((f[j]==0)&&(d[j]<s))
{
t=j;
s=d[j];
}
if (t==-1) break;
f[t]=1;
for (j=0;j<n;j++)
if (f[j]==0)
if (d[j]>d[t]+a[t][j])
{
d[j]=d[t]+a[t][j];
c[j]=c[t]+b[j];
e[j]=e[t];
}
else
if (d[j]==d[t]+a[t][j])
{
e[j]+=e[t];
if (c[j]<c[t]+b[j]) c[j]=c[t]+b[j];
}
}
cout<<e[c2]<<' '<<c[c2];
return 0;
}

DFS

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,s,t,maxt,mind,c1,c2,d,a[501][501],b[501]={0},c[501]={0};
void dfs(int k)
{
int i,j;
if (k==c2)
{
if (d<mind)
{
mind=d;
maxt=t;
s=1;
}
else if (d==mind)
{
s++;
if (t>maxt) maxt=t;
}
}
c[k]=1;
for (i=0;i<n;i++)
if ((c[i]==0)&&(a[k][i]>0))
{
d+=a[k][i];
t+=b[i];
dfs(i);
d-=a[k][i];
t-=b[i];
}
c[k]=0;
}
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k;
memset(a,-1,sizeof(a));
cin>>n>>m>>c1>>c2;
for (i=0;i<n;i++) cin>>b[i];
for (i=0;i<m;i++)
{
cin>>j>>k>>t;
a[j][k]=a[k][j]=t;
}
maxt=0;
mind=0x7FFFFF;
t=b[c1];
d=0;
dfs(c1);
cout<<s<<' '<<maxt;
return 0;
}

提交记录

dijsktra

DFS

PAT (Advanced Level) Practise 1003 解题报告的更多相关文章

  1. PAT (Advanced Level) Practise 1004 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 1600 ...

  2. PAT (Advanced Level) Practise 1002 解题报告

    GitHub markdownPDF 问题描述 解题思路 代码 提交记录 问题描述 A+B for Polynomials (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 ...

  3. PAT (Advanced Level) Practise 1001 解题报告

    GiHub markdown PDF 问题描述 解题思路 代码 提交记录 问题描述 A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判 ...

  4. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  5. PAT (Advanced Level) Practise - 1094. The Largest Generation (25)

    http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...

  6. 1079. Total Sales of Supply Chain (25)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1079. Total Sales of Supply Chain (25) 时间限制250 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is ...

  7. 1076. Forwards on Weibo (30)【树+搜索】——PAT (Advanced Level) Practise

    题目信息 1076. Forwards on Weibo (30) 时间限制3000 ms 内存限制65536 kB 代码长度限制16000 B Weibo is known as the Chine ...

  8. 1064. Complete Binary Search Tree (30)【二叉树】——PAT (Advanced Level) Practise

    题目信息 1064. Complete Binary Search Tree (30) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B A Binary Search Tr ...

  9. 1078. Hashing (25)【Hash + 探測】——PAT (Advanced Level) Practise

    题目信息 1078. Hashing (25) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B The task of this problem is simple: in ...

随机推荐

  1. axis 数据流

    产生数据流的代码 模板   重新修改了下 :]axis_data_cnt='d0; :]axis_data_frame_cnt='d0; :]delay_cnt='d0; initial begin ...

  2. Linux编程学习笔记(二)

    续上个章节,这个章节主要是Linux的远程登录系统操作笔记 一. Linux一般作为服务器使用,但是服务器都是在机房的,所以不可能经常跑到机房去操作系统,所以使用远程登录系统,在Linux的系统一般使 ...

  3. cf478d 线性dp好题

    /* 给定r个红块,g个绿块,按要求堆放 问当堆放成最大高度时,有多少种可能的堆放方式 排列要求:1.第i行放i块 2.每行同色 首先当然要确定能够放置几行 设红块有r个,绿块有g个,那么放置h行需要 ...

  4. 第一周学习总结-Java

    2018年7月15日 暑假第一周,我从网上找了一些讲Java的视频,学到了一些Java的基础,同时也弥补了一些之前学c/c++的知识漏洞.例如,了解到了原码反码补码和按位取反运算符(~)的运算原理. ...

  5. excel生成数据

    Sub function1()Dim i As LongFor i = 1 To 1000000Cells(i, 1) = "A" & iCells(i, 2) = &qu ...

  6. txt文档去重复内容

    @echo off for /f "delims=" %%i in ('type "%1"') do (if not defined %%i set %%i=A ...

  7. redis 单实例安装

    单实例安装 近些年,由于内存技术的提升.造价的下降,越来越多企业的服务器内存已增加到几百G.这样的内存容量给了内存数据库一个良好的发展环境. 而使用Redis是内存数据库的一股清流,渐有洪大之势.下面 ...

  8. linux下配置docker和splash(图文)

    所需要环境:ubuntu16.04 第一步用:sudo apt install docker.io 第二步:完成后查看一下有没有成功 命令:docker -v,如果是输入错了写成了大V他会提示你有哪些 ...

  9. python property的用法

    用法一: class Test(object): def __init__(self): # 私有化 self.__num = 100 #名字重整_Test__num def setNum(self, ...

  10. last与lastb命令 读取的日志文件

    在linux系统中,last与lastb命令用来列出目前与过去登录系统的用户相关信息.指令英文原义: last, lastb - show listing of last logged in user ...