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 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.
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 1Sample Output
2 4
第一行给出城镇数目、道路数目、起点、终点;
第二行给出各个城镇救援队的数量;
以下各行给出两个城镇之间的距离;
沿路可以收集经过城镇的救援队,计算出起终点之间不同的最短路的数量和最大的救援队数量。
用直捣黄龙 http://blog.csdn.net/mikasa3/article/details/65448765 的代码稍微改改过的…
#include<bits/stdc++.h>
using namespace std;
#define INF 0xfffffff
#define MAXN 1010
int st,en;
struct Node
{
int dist;//最短路长度
int cnt;//援助数
} a[MAXN];//对于每个城镇
int n,m;//顶点数和边数
bool used[MAXN];//是否使用过
int dis[MAXN];//最短路的数量
int pre[MAXN];//pre[i]表示v0到vi的最短路径上vi的前一个顶点的序号
int num[MAXN];//援助数
int cost[MAXN][MAXN];//边权
void dijkstra(int s)//s到其它点的最短路
{
for(int i=0; i<=n; ++i)//初始化
{
a[i].dist=INF;
a[i].cnt=0;
dis[i]=0;
used[i]=false;
pre[i]=-1;
}
a[s].cnt=num[s];
a[s].dist=0;//起始点的最短路是0
dis[s]=1;//起始点的最短路数是1
while(true)
{
int v=-1,minx=INF;
for(int u=0; u<n; u++)//从未使用过的顶点中选择一个距离最小的顶点
if(!used[u]&&minx>a[u].dist)
{
minx=a[u].dist;
v=u;
}
if(v==-1) break;
used[v]=true;
for(int u=0; u<n; ++u)
{
if(used[u]) continue;
if(a[v].dist+cost[v][u]<a[u].dist)//路径更多
{
a[u].dist=a[v].dist+cost[v][u];//更新最短路
a[u].cnt=a[v].cnt+num[u];//加入在当前城镇的援助数更新
pre[u]=v;//记录路径
dis[u]=dis[v];//最短路数量
}
else if(cost[v][u]!=INF&&cost[v][u]+a[v].dist==a[u].dist)
{
dis[u]+=dis[v];//更新最短路数量
if(a[u].cnt<a[v].cnt+num[u])//援助数更多
{
a[u].dist=cost[v][u]+a[v].dist;
a[u].cnt=a[v].cnt+num[u];
pre[u]=v;
}
}
}
}
}
vector<int> get_path(int t)//输出路径,计算到顶点n的最短路
{
vector<int> path;
for(; t!=-1; t=pre[t])
path.push_back(t);
reverse(path.begin(),path.end());
return path;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int i;
cin>>n>>m;
cin>>st>>en;//起终点城镇名
for(i=0; i<n; ++i)
cin>>num[i];//记录援助数量
for(int i=0; i<=n; ++i)//初始化
for(int j=0; j<=n; ++j)
cost[i][j]=INF;
for(i=0; i<m; i++)
{
int s,ss;
int t;
cin>>s>>ss>>t;
cost[s][ss]=t;//无向图
cost[ss][s]=t;
}
dijkstra(st);
//路径还原,输出路径
/*vector<int> path;
path=get_path(en);//输出路径
int len=path.size();
for(int i=0; i<len-1; ++i)
cout<<path[i]<<"->";
cout<<path[len-1]<<endl;*/
cout<<dis[en]<<" "<<a[en].cnt<<endl;//输出最短路径的条数、援助总数
return 0;
}