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

1003. 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

点似乎不是很多,数据友好,最后一组7MS都能跑完,另外一种感觉麻烦一点但是适应性比较好做法是修改一下dij的写法加上dp的思想,然而不太懂,有空再看……

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=510;
const int M=500010;
struct info
{
	int to;
	int pre;
	int dx;
};
info E[M];
int head[M],ne;
int val[N],d[N],vis[N];
int maxm,ans,n,m,s,t;
void add(int s,int t,int d)
{
	E[ne].to=t;
	E[ne].pre=head[s];
	E[ne].dx=d;
	head[s]=ne++;
}
void init()
{
	CLR(head,-1);
	ne=0;
	CLR(val,0);
	CLR(d,INF);
	ans=0;
	maxm=0;
	CLR(vis,0);
}
void spfa(int s)
{
	int i;
	priority_queue<pii>Q;
	d[s]=0;
	Q.push(pii(-d[s],s));
	while (!Q.empty())
	{
		int now=Q.top().second;
		Q.pop();
		for (i=head[now]; ~i; i=E[i].pre)
		{
			int v=E[i].to;
			if(d[v]>d[now]+E[i].dx)
			{
				d[v]=d[now]+E[i].dx;
				Q.push(pii(-d[v],v));
			}
		}
	}
}
void dfs(int now,int len,int ma)
{
	int i;
	if(now==t)
	{
		if(ma>maxm)
			maxm=ma;
		++ans;
		return ;
	}
	for (i=head[now]; ~i; i=E[i].pre)
	{
		int v=E[i].to;
		if(!vis[v]&&d[now]+E[i].dx==d[v])
		{
			vis[v]=1;
			dfs(v,d[v],ma+val[v]);
			vis[v]=0;
		}
	}
}
int main(void)
{
	int x,y,z,i,j;
	while (~scanf("%d%d%d%d",&n,&m,&s,&t))
	{
		init();
		for (i=0; i<n; ++i)
			scanf("%d",&val[i]);
		for (i=0; i<m; ++i)
		{
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
			add(y,x,z);
		}
		spfa(s);
		dfs(s,d[s],val[s]);
		printf("%d %d\n",ans,maxm);
	}
	return 0;
}
时间: 2024-12-28 03:25:14

PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)的相关文章

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

1003 Emergency(25 分)C语言版本(提问求解答)

1003 Emergency(25 分) 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 an

1003. Emergency (25)——PAT (Advanced Level) Practise

题目信息: 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.

PAT (Basic Level) Practise 1003. 我要通过!

1003. 我要通过! 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1. 字符串中必须仅有P, A, T这三种字符,不可以包含其它字符:2. 任意形如 xPATx 的字符串都可以获得“答案正确”,其中 x 或者是

1106. Lowest Price in Supply Chain (25)【树+深搜】——PAT (Advanced Level) Practise

题目信息 1106. Lowest Price in Supply Chain (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer. Starting from on

1062. Talent and Virtue (25)【排序】——PAT (Advanced Level) Practise

题目信息 1062. Talent and Virtue (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about people's talent and virtue. According to his theory, a man being outstanding i

1004. Counting Leaves (30)——PAT (Advanced Level) Practise

题目信息: 1004. Counting Leaves (30) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child. Input Each input file contai

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 Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less tha

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 Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with follo