題目:
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, 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 Specification:
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
解題思路:
使用迪杰斯特拉算法肛捍,需要注意統(tǒng)計(jì)保存最短路徑的數(shù)量之众,最后選擇權(quán)值最高(途徑結(jié)點(diǎn)救援隊(duì)數(shù)量總和最多)的路徑。
代碼:
編譯器:C++(g++)
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> RoadInfo;
//查找未被訪問過的棺禾,距離c1路徑長度最小的結(jié)點(diǎn)
int findMin(vector<RoadInfo> &roadLst)
{
int min=-1,index=-1;
int n=roadLst.size();
for(int i=0;i!=n;++i)
{
if(roadLst[i][n+2]!=1&&roadLst[i][n+1]!=-1)
{
if(-1==min)
{
min=roadLst[i][n+1];
index=i;
}
else
{
if(roadLst[i][n+1]<min)
{
min=roadLst[i][n+1];
index=i;
}
}
}
}
if(index!=-1)
roadLst[index][n+2]=1;
return index;
}
int main()
{
int n,m,c1,c2;
cin>>n>>m>>c1>>c2;
vector<int> numOfCity;
for(int i=0;i!=n;++i)
{
int tmp;
cin>>tmp;
numOfCity.push_back(tmp);
}
vector<vector<int>> road;
for(int i=0;i!=m;++i)
{
int t1,t2,t3;
cin>>t1>>t2>>t3;
vector<int> t;
t.push_back(t1);
t.push_back(t2);
t.push_back(t3);
road.push_back(t);
//swap一下t[0]和t[1]再push,這樣后面只用遍歷t[0]就可以了
std::swap(t[0],t[1]);
road.push_back(t);
}
//c1到此節(jié)點(diǎn)的路程信息,0~n-1表示經(jīng)過的節(jié)點(diǎn)蛀醉,n為同等長度的路徑數(shù)量,n+1為路徑長度,n+2為是否以此節(jié)點(diǎn)為中心訪問過
RoadInfo ri(n+3,-1);
vector<RoadInfo> roadLst(n,ri);
for(int i=0;i!=2*m;++i)
{
//以c1為出發(fā)點(diǎn)遍歷
if(c1==road[i][0])
{
roadLst[road[i][1]][c1]=1;
roadLst[road[i][1]][n]=1;
roadLst[road[i][1]][n+1]=road[i][2];
}
}
roadLst[c1][n]=1; //c1到c1的路徑只有1條
roadLst[c1][n+1]=0; //c1到c1的距離為0
roadLst[c1][n+2]=1; //將c1結(jié)點(diǎn)置為已訪問狀態(tài)
while(1)
{
int min=findMin(roadLst); //查找未被訪問過的滞欠,距離c1路徑長度最小的結(jié)點(diǎn)
if(-1==min||min==c2) //如果min==c2,則表明此時(shí)c1到c2的最短路徑已經(jīng)確認(rèn)
{
break;
}
for(int i=0;i!=2*m;++i)
{
if(min==road[i][0])
{
if(-1==roadLst[road[i][1]][n+1])
{
roadLst[road[i][1]]=roadLst[min];
roadLst[road[i][1]][min]=1;
roadLst[road[i][1]][n+1]+=road[i][2];
roadLst[road[i][1]][n+2]=-1;
}
else
{
if(roadLst[road[i][1]][n+1]>roadLst[min][n+1]+road[i][2])
{
int visited=roadLst[road[i][1]][n+2];
roadLst[road[i][1]]=roadLst[min];
roadLst[road[i][1]][min]=1;
roadLst[road[i][1]][n+1]+=road[i][2];
roadLst[road[i][1]][n+2]=visited;
}
//路徑長度相同筛璧,則比較哪條路上的救援隊(duì)數(shù)量多惹恃,選擇多的那一條
else if(roadLst[road[i][1]][n+1]==roadLst[min][n+1]+road[i][2])
{
int sumOfRoad=roadLst[road[i][1]][n]+roadLst[min][n];
int curTeams=0,preTeams=0;
for(int j=0;j!=n;++j)
{
if(1==roadLst[road[i][1]][j])
preTeams+=numOfCity[j];
if(1==roadLst[min][j])
curTeams+=numOfCity[j];
}
curTeams+=numOfCity[min];
if(curTeams>preTeams)
{
int visited=roadLst[road[i][1]][n+2];
roadLst[road[i][1]]=roadLst[min];
roadLst[road[i][1]][min]=1;
roadLst[road[i][1]][n+1]+=road[i][2];
roadLst[road[i][1]][n+2]=visited;
}
roadLst[road[i][1]][n]=sumOfRoad;
}
}
}
}
}
int teams=0;
for(int j=0;j!=n;++j)
{
if(1==roadLst[c2][j])
teams+=numOfCity[j];
}
teams+=numOfCity[c2];
cout<<roadLst[c2][n]<<" "<<teams<<endl;
return 0;
}