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. Ribbon服务消费者

    springcloud使用到两种消费工具,ribbon和feign ribbon实现了服务的负载均衡 feign默认集成了ribbon,一般情况下使用feign作为消费端 搭建消费者项目(Ribbon ...

  2. github文档

    Video Guides GitHub Help GitHub.com   Hello World 10 minute read Intro What is GitHub? Create a Repo ...

  3. CF 1042F

    玄学贪心... 题意:给出一棵树,要求将他的所有叶节点分成最少的组,且在每组中的任意两节点之间的距离不大于k 解析: 显然是个贪心啦... 稍微考虑一下贪心思想: 我们从下向上合并整棵树,在合并到某个 ...

  4. Nginx详解七:Nginx基础篇之Nginx官方模块

    Nginx官方模块 --with-http_stub_status_module:Nginx的客户端状态,用于监控连接的信息,配置语法如下:配置语法:stub_status;默认状态:-配置方法:se ...

  5. ajax----发送异步请求的步骤

    1)获取(创建)Ajax对象:获取XMLHttpRequest对象2)创建请求:调用xhr的open方法3)在发送请求之前需要设置回调函数:绑定指定xhr的onreadystatechange事件4) ...

  6. Tomcat配置域名/IP访问及其中遇到的问题注意事项

    1.先在tomcat下的conf下找到server.xml文件,用记事本打开后,首先对端口号进行修改,以前一直以为8080是默认的端口号,其实默认的端口号是80 <Connector port= ...

  7. ORA-12519 ORA-12516

    目录: 错误信息 原因分析 解决方法 1. 错误信息 [oracle@test1 admin]$ oerr ora , , "TNS:no appropriate service handl ...

  8. Android NDK笔记

    目录 Android NDK笔记 AOSP Android repository Android SDK / SDK Tools NDK cmake && ninja lldb adb ...

  9. [转] 理解Object.defineProperty的作用

    对象是由多个名/值对组成的无序的集合.对象中每个属性对应任意类型的值.定义对象可以使用构造函数或字面量的形式: var obj = new Object; //obj = {} obj.name = ...

  10. IIS异常

    http 错误 500.19 - internal server error 今天发布wcf到本地的IIS上,访问时出现了500.19错误.有效解决办法:是因为IIS没有安装完全,把能勾选的全部勾选上 ...