1003 Emergency

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 Specification:
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, C
​1
​​ and C
​2
​​ - 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 c
​1
​​ , c
​2
​​ 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 C
​1
​​ to C
​2
​​ .

Output Specification:
For each test case, print in one line two numbers: the number of different shortest paths between C
​1
​​ and C
​2
​​ , 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
respectively
adv. 分别; 各自; 顺序为; 依次为;
scattered
adj. 分散的; 零散的; 疏落的;
v. 撒; 撒播; 散开; 四散; 使分散; 驱散;
看不懂,六级飘过的渣渣
典型的dijkstra模板题,我做的时候没注意要双向图。。这是一个问题,确实,首先题目没说,但是我总觉得是单向图,如果是单向图有些点就没法到达
朴素版

#include<iostream>
const int maxn =505;
const int INF =0x3f3f3f3f;
using namespace std;
int G[maxn][maxn],w[maxn],num[maxn],weight[maxn],d[maxn];
int N,M,C1,C2;	
bool vis[maxn]={false};
void dijkstra(int s){
	fill(d,d+maxn,INF);
	d[s] = 0;
	num[s] =1;
	w[s] = weight[s];
	for(int i=0;i<N;i++){
		int u=-1,min=INF;
		for(int j=0;j<N;j++){
			if(!vis[j] && d[j] < min){
				u=j;
				min=d[j];
			}
		}
		if(u == -1) return ;
		vis[u] = true;
		for(int i=0;i<N;i++){
			if(!vis[i] &&G[u][i] !=INF ){
				if(d[u] + G[u][i] < d[i]){
					d[i] = d[u] +G[u][i];
					w[i] = w[u] + weight[i];
					num[i] = num[u];
				}
				else if(d[u] + G[u][i] == d[i]){
					if(weight[i] + w[u] > w[i]){
						w[i] =w[u] + weight[i];
					}
					num[i]+=num[u];
				}
			}
		}
	}
}
int main(){
	cin>>N>>M>>C1>>C2;
	fill(G[0],G[0]+maxn*maxn,INF);
	for(int i=0;i<N;i++) cin>>weight[i];
	for(int i=0;i<M;i++){
		int x,y,z;
		cin>>x>>y>>z;
		G[x][y]=z;
		G[y][x]=z;
	}
	dijkstra(C1);
	cout<<num[C2]<<' '<<w[C2];
	
	
	return 0;
} 

堆优化

#include<iostream>
#include<vector>
#include<queue>
const int maxn =505;
const int INF =0x3f3f3f3f;
using namespace std;
typedef pair<int,int> edge;
struct Node{
	int v,dis;
	Node(int v,int dis) :v(v),dis(dis) {};
	bool operator <(const Node other)const{
	   return	dis > other.dis;
	 }
};
int w[maxn],num[maxn],weight[maxn],d[maxn];
int N,M,C1,C2;	
bool vis[maxn]={false};
vector<edge> graph[maxn];
int main(){
	cin>>N>>M>>C1>>C2;
	fill(d,d+maxn,INF);
	for(int i=0;i<N;i++) cin>>weight[i];
	
	for(int i=0;i<M;i++){
		int x,y,z;
		cin>>x>>y>>z;
		graph[x].push_back(edge(y,z));
		graph[y].push_back(edge(x,z));
	}
	d[C1]=0;
	w[C1]=weight[C1];
	num[C1]=1;
	priority_queue<Node> q;
	q.push(Node(C1,0));
	while(!q.empty()){
		int v=q.top().v;
		q.pop();
		if(v == C2) break;
		vis[v] = true;
		for(int i=0;i<graph[v].size();i++){
			edge u=graph[v][i];
			int vt=u.first,dist=u.second;
			if(vis[vt]) continue;
			if(d[v] + dist < d[vt] ) {
				d[vt] = d[v] + dist;
				q.push(Node(vt,d[vt]));
				w[vt] =weight[vt] + w[v];
				num[vt] =num[v];
			}
			else if(d[v] + dist == d[vt]) {
				w[vt] = max(w[vt], w[v] +weight[vt]);
				num[vt]+=num[v];
			}
			
		}
		
	}
	cout<<num[C2]<<' '<<w[C2];
	return 0;
} 

堆优化其实就是优化每次查找距离源点最近的点,用优先队列能使时间复杂度降低,不难理解,要把你自己定义的结构体,pair的意义要弄清楚,不然写的时候容易乱

===================================================================
一般不会考的那么裸的dijkstra,最短路径一般是第一标尺,
第二标尺一般有3种
1.
给每条边再增加一条边权,比如花费什么的

cost[u][v] 表示 u->v的花费,增加一个c[]数组为源点到u点的最少花费为c[u]
  for(int v=0;v<n;v++){
  	//如果v未访问 && u能到达v
	  if(!vis[v] && g[u][v]!=INF){
	  	if(d[u] + g[u][v] < d[v]){
	  		d[v] =d[u] +g[u][v];
	  		c[v] =c[u] +cost[u][v];
		  }
		  else if(d[u] + g[u][v] == d[v]){
		  	 if(c[u] + cost[u][v] < c[v]) c[v] = c[u] +cost[u][v];
		  }
	  } 
  }

2
给每个点增加一个点权,就像这题,给每个城市增加一个小组人数

for(int v=0;v<n;v++){
  	//如果v未访问 && u能到达v
	  if(!vis[v] && g[u][v]!=INF){
	  	if(d[u] + g[u][v] < d[v]){
	  		d[v] =d[u] +g[u][v];
	  		w[v] =w[u] +weight[u][v];
		  }
		  else if(d[u] + g[u][v] == d[v]){
		  	 if(w[u] + weight[u][v] < w[v]) w[v] = w[u] +weight[u][v];
		  }
	  } 
  }

3
直接问有几条最短路径,这题也有

for(int v=0;v<n;v++){
  	//如果v未访问 && u能到达v
	  if(!vis[v] && g[u][v]!=INF){
	  	if(d[u] + g[u][v] < d[v]){
	  		d[v] =d[u] +g[u][v];
	  		num[v]=num[u];
		  }
		  else if(d[u] + g[u][v] == d[v]){
		  	 num[v]+=num[u];
		  }
	  } 
  }

===================================================================
判断的前提是if(!vis[v] && g[u][v]!=INF) 这题就是2和3的组合
参考堆优化
算法笔记P378

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值