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. Python关于类的实操

    实操一:总结 1.什么是绑定到对象的方法,如何定义,如何调用,给谁用?有什么特性? 2.什么是绑定到类的方法,如何定义,如何调用,给谁用?有什么特性? 3.什么是解除绑定的函数,如何定义,如何调用,给 ...

  2. java易错题----静态方法的调用

    class A{ public static String s="A.s"; } class B extends A{ public static String s="B ...

  3. zoj3471 状态压缩dp基础

    /* dp[S]表示状态S下的最大收益,0表示没有了,1表示还在 */ #include<bits/stdc++.h> using namespace std; <<],mp[ ...

  4. SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

    2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...

  5. Python知乎热门话题数据的爬取实战

    import requestsfrom pyquery import PyQuery as pq url = 'https://www.zhihu.com/explore'headers = { 'u ...

  6. servlet保存会话数据---利用隐藏域

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletExcep ...

  7. 激活函数的比较,sigmoid,tanh,relu

    1. 什么是激活函数 如下图,在神经元中,输入inputs通过加权.求和后,还被作用了一个函数.这个函数就是激活函数Activation Function 2. 为什么要用激活函数 如果不用激活函数, ...

  8. salt 执行shell 脚本 修改名字

    #!/bin/bash #命令 匹配主机 执行模块 脚本路径,必须放在salt目录下 源主机名 修改的主机名 #salt tt_LD_LM_LC_1 cmd.script salt://init/re ...

  9. 查找所有sphinx引擎表并生成创建表的语句

    -- 查找所有sphinx引擎select group_concat(table_name separator ' ') from information_schema.tables where en ...

  10. autofac 创建实例方法总结

    1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure the component so tha ...