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 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
注: 这是一个无向图。
思路:单源最短路径且无负边权(dijkstar算法)
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int n,m,start,e;
const int inf=100000000;
int dvi[507];//最短的最大权值
int vi[507];//点权
int g[507][507];//边权
bool v[507];
int d[507];//起点到每个点的最短距离
int ans[507];//纪录每个点最短路径的条数,初始化为0
void dijkstra(int x)
{
fill(d,d+507,inf);
fill(ans,ans+507,0);
d[x]=0;
ans[x]=1;
for(int i=0;i<n;i++)
{
int u=-1,kk=inf;
for(int j=0;j<n;j++)//找到距离起点最小的点
{
if(v[j]==false&&d[j]<kk)
{
kk=d[j];
u=j;
}
}
if(u==-1)return ;//此时没有联通
v[u]=true;
for(int i=0;i<n;i++)
{
if(v[i]==false && g[u][i]!=inf && d[u]+g[u][i]<=d[i] )
{
if(d[i]==d[u]+g[u][i] )//是否需要更新最大点权和
{
dvi[i]=max(dvi[i],dvi[u]+vi[i]);
ans[i]+=ans[u];
continue;
}
else//因为最短路的优先级更高,跟新点权和,并且跟新ans[i]=ans[u]
{
d[i]= d[u]+g[u][i];
dvi[i]=dvi[u]+vi[i];
ans[i]=ans[u];
}
}
}
}
}
int main()
{
fill(g[0],g[0]+507*507,inf);
scanf("%d%d%d%d",&n,&m,&start,&e);
for(int i=0;i<n;i++)
{
scanf("%d",&vi[i]); //读入点权
dvi[i]=vi[i];
}
for(int i=0;i<m;i++)//读入边权
{
int x,y,b;
scanf("%d%d%d",&x,&y,&b);
g[x][y]=b;
g[y][x]=b;
}
dijkstra(start);
cout<<ans[e]<<" "<<dvi[e]<<endl;
return 0;
}
使用邻接表:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
struct node
{
int s,bi;//当前节点和其他节点的关系和边权
};
vector<node> g[507];
int vi[507];//点权
int ans[507];//最短路径的数量
int dis[507];//最短路径的长度
int dd[507];//最短路径上的最大点权值
bool v[507];
int n,m,start,e;
const int inf=100000000;
void dijkstra()
{
dd[start]=vi[start];
fill(dis,dis+507,inf);//不要忘记初始化距离
dis[start]=0;
ans[start]=1;
for(int i=1;i<n;i++)
{
int u=-1,Min=inf;
for(int j=0;j<n;j++)
{
if(v[j]==false &&dis[j]<Min )
{
u=j;
Min=dis[j];
}
}
if(u==-1)return ;
v[u]=true;
for(int z=0;z<g[u].size();z++)
{
int k=g[u][z].s;
if(v[k]==false && dis[k]>=dis[u]+g[u][z].bi )
{
if(dis[k]==dis[u]+g[u][z].bi)
{
ans[k]+=ans[u];
dd[k]=max(dd[k],dd[u]+vi[k]);
}
else
{
dis[k]=dis[u]+g[u][z].bi;
ans[k]=ans[u];
dd[k]=dd[u]+vi[k];
}
}
}
}
}
int main()
{
node k,k1;
scanf("%d%d%d%d",&n,&m,&start,&e);
for(int i=0;i<n;i++)
scanf("%d",&vi[i]);
for(int i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
k.s=y,k.bi=z;
k1.s=x,k1.bi=z;
g[x].push_back(k);
g[y].push_back(k1);
}
dijkstra();
// for(int i=0;i<n;i++)
// {
// cout<<ans[i]<<" "<<dis[i]<<" "<<dd[i]<<endl;
// }
cout<<ans[e]<<" "<<dd[e]<<endl;
return 0;
}