1003. Emergency (25)

时间限制
400 ms
内存限制
32000 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

参考http://www.cnblogs.com/yanhaiming/archive/2012/12/01/2797064.html

#include<iostream>
#include<cstring>
using namespace std;

const int N = 1000;
const int INF = 100000000;

//图的邻接矩阵
int road[N][N];

//标记某一节点是否被访问过
bool isVisited[N];

//记录每一节点的权值
int team_num[N];

int dis[N];

//最短路径的数目
int path_num =0;

//最大的医疗队的数目
int g_max_team_num = 0;

void initData()
{
int i,j;
for(i=0; i<N; i++)
{
isVisited[i] = false;
team_num[i] = 0;
dis[i] = INF;//初始化为无穷大.
for(j=0; j<N; j++)
{
road[i][j] = INF;
road[j][i] = INF;
}
}
}

//单源最短路径算法。
void Dijstra(int n,int src, int des)
{
int i,j;
for(i=0; i<n; i++)
dis[i] = road[src][i];
isVisited[src] = true;

for(i=0; i<n-1; i++)//最多循环n-1次就足够了,选n-1个最小值。
{
int minDis = INF;
int cur = 0;
for(j=0; j<n; j++)
if(!isVisited[j] && dis[j]<minDis)
{
minDis = dis[j];
cur = j;
}
if(minDis == INF) //已经完成了连通路径的遍历。
return;
//dis[cur]为dis数组中的最小值,访问节点cur.
isVisited[cur] = true;
//更新Dis数组的内容.
for(j=0; j<n; j++)
if(road[cur][j] <INF && dis[j] > dis[cur] + road[cur][j])
dis[j] = dis[cur] + road[cur][j];
}
}
//深度搜索来得到最短路径的数目。
void dfs(int n,int cId,int des,int curDis,int curTeamsNum)
{
isVisited[cId] = true;
if(cId == des)
{
if(curDis == dis[des]) //找到一条最短路径
{
path_num++;//最短路径数目加1
if(curTeamsNum > g_max_team_num)
g_max_team_num = curTeamsNum;
}
return;
}
if(curDis > dis[des]) //当前的路径长度已经超过最短路径,就没有必要继续搜索了。
return;
//从城市cId开始搜索
for(int i=0; i<n; i++)
{
/*
if(!isVisited[i] && road[cId][i] < INF)//如果城市i没有被访问过,且cId到i连通。
{
//isVisited[i] = true;
dfs(n,i,des,curDis+road[cId][i],curTeamsNum+team_num[i]);
isVisited[i] = false;
}
*/
//这样的剪枝比上一种更加强大。
if(dis[cId] + road[cId][i] == dis[i])
dfs(n,i,des,curDis+road[cId][i],curTeamsNum+team_num[i]);
}
}

int main()
{
int i,j,n,m,c1,c2,L,src,des;

initData();

cin>>n>>m>>src>>des;
for(i=0; i<n; i++)
cin>>team_num[i];
for(i=0; i<m; i++)
{
cin>>c1>>c2>>L;
road[c1][c2] = L;
road[c2][c1] = L;
}

Dijstra(n,src,des);

//重置各city的被访问状态。
for(i=0; i<n; i++)
isVisited[i] = false;

dis[src] = 0;
dfs(n,src,des,0,team_num[src]);

cout<<path_num<<" "<<g_max_team_num<<endl;
return 0;
}

浙大 pat 1003 题解的更多相关文章

  1. 浙大pat 1035题解

    1035. Password (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To prepare f ...

  2. 浙大pat 1025题解

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  3. 浙大pat 1011题解

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  4. 浙大PAT 7-06 题解

    #include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> ...

  5. 浙大pat 1012题解

    1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...

  6. 浙大 pat 1038 题解

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  7. 浙大 pat 1047题解

    1047. Student List for Course (25) 时间限制 400 ms 内存限制 64000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. 浙大pat 1054 题解

    1054. The Dominant Color (20) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Behind the scen ...

  9. 浙大pat 1059 题解

    1059. Prime Factors (25) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

随机推荐

  1. iOS开发技术分享(1)— iOS本地数据存储

    iOS开发技术分享(1)— iOS本地数据存储 前言: 我本是一名asp.net程序员,后来加入了iOS游戏开发队伍,到现在也有一年多的时间了.这一年来,每天都干到2.3点钟才睡觉,不为别的,只为了学 ...

  2. Citrix 服务器虚拟化之六 Xenserver虚拟机创建与快照

    Citrix 服务器虚拟化之六  Xenserver虚拟机创建与快照 在Xenserver上可以创建Windows和Linux等虚拟机,Xenserver支持大部分的主流操作系统,可以使用 XenCe ...

  3. 64位CentOS 6.0下搭建LAMP环境

    系统环境:Centos6.0 x64 1.确认搭建LAMP所需要的环境是否已经安装 [root@centos6 ~]# rpm -q make gcc gcc-c++ zlib-devel libai ...

  4. 分享.net常见的内存泄露及解决方法

    分享.net常见的内存泄露及解决方法 关于内存泄漏的问题,之前也为大家介绍过,比如:<C++中内存泄漏的检测方法介绍>,是关于C++内存泄漏的.今天为大家介绍的是关于.NET内存泄漏的问题 ...

  5. [珠玑之椟]估算的应用与Little定律

    [珠玑之椟]估算的应用与Little定律 估算的数据主要依赖于所能获得的数据和常识,有时还包括实践而不仅仅是理论.它常常作为一个大问题中的子问题,恰当地估算可以省去精确计算的时间和开销.在计算机领域, ...

  6. java链接mysql数据库

    package com.DateSystem; import java.sql.Connection; import java.sql.DriverManager; import java.sql.S ...

  7. PPT资料下载 - 问题驱动的软件测试设计:强化测试用例设计

    测试用例设计是整个软件测试过程中非常重要的测试活动,需求规格说明是测试人员开展测试设计的主要参考输入.而在测试实践中基于需求规格说明得到的测试用例,在测试覆盖率.测试效率.测试有效性和测试质量等方面的 ...

  8. Android开发之简单的电子相册实现

    电子相册的效果图和结构图: 图片资源的文件: package com.example.electronicalbum; public interface ImageResource {   //用一个 ...

  9. Linux 内核源码中likely()和unlikely()

    ikely()与unlikely()在2.6内核中,随处可见,那为什么要用它们?它们之间有什么区别呢? 首先明确: if (likely(value))等价于if (value)if (likely( ...

  10. 游戏对象、组件和Prefabs

    如标题所言,本文由3个部分组成,分别讲述游戏对象.组件和Prefabs(预设体). 1. 游戏对象 任何游戏对象都由组件组成,组件是实现一切功能所必需的.我们创建的对象会在Hierarchy视图中显示 ...