1003 Emergency (25)(25 分)
As an emergency rescue team leader of a city, you are given a specialmap of your country. The map shows several scattered cities connected bysome roads. Amount of rescue teams in each city and the length of eachroad between any pair of cities are marked on the map. When there is anemergency call to you from some other city, your job is to lead your mento the place as quickly as possible, and at the mean time, call up asmany hands on the way as possible.
Input
Each input file contains one test case. For each test case, the firstline 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, C1and C2 - the cities that you are currently in and that you must save,respectively. The next line contains N integers, where the i-th integeris 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 thepair of cities connected by a road and the length of that road,respectively. It is guaranteed that there exists at least one path fromC1 to C2.
Output
For each test case, print in one line two numbers: the number ofdifferent shortest paths between C1 and C2, and the maximum amount ofrescue teams you can possibly gather.\All the numbers in a line must be separated by exactly one space, andthere 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
思路:dijkstra算法,优先队列维护
代码如下:
#include<bits/stdc++.h>
using namespace std;
int team[510],vis[510];//vis[i]记录开始点到i点所需要的最短距离
struct pp
{
int e,d,t;
};
bool operator< (const pp&a,const pp&b)
{
if(a.d!=b.d)
return a.d>b.d;
}
priority_queue<pp>q;
vector<vector<pp> >v;
int main()
{
int n,m,s,e,d=INT_MAX,t=0,l=0,flag=0;
pp p;
cin>>n>>m>>s>>e;
for(int i=0;i<n;i++)
cin>>team[i];
v.resize(n);
memset(vis,0,sizeof(vis));
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b>>p.d;
p.e=b;
p.t=team[b];
v[a].push_back(p);
p.e=a;
p.t=team[a];
v[b].push_back(p);
}
p.e=s;
p.d=0;
p.t=team[s];
q.push(p);
while(!q.empty())
{
p=q.top();q.pop();
if(p.d>vis[p.e]&&vis[p.e]!=0)continue;
vis[p.e]=p.d;
if(p.e==e&&p.d<=d)
{
d=p.d;
l++;
if(p.t>t)
t=p.t;
}
if(p.d>d)
break;
for(int i=0;i<v[p.e].size();i++)
{
pp l=v[p.e][i];
l.d+=p.d;
if(l.d>d)continue;
if(l.d>vis[l.e]&&vis[l.e]!=0)continue;
l.t+=p.t;
q.push(l);
}
}
cout<<l<<" "<<t<<endl;
while(!q.empty())
q.pop();
return 0;
}