您的位置:首页 > 其它

csu1024 Mining (dijkstra)

2012-05-17 13:49 148 查看

1024: Mining

Time Limit: 1 Sec Memory Limit: 128 MB
SUBMIT: 14 Solved: 4
[SUBMIT][STATUS]

Description

Zfy and Kevin are friends and they like to go mining together at some place in the university. About at 6 p.m. they start to feel tired and want to go to dormitory. They want to get to dormitory quickly so each of them chooses a path that minimizes the distance to his dormitory. However, Zfy and Kevin also like to walk together in order that they can talk about the technique of mining,. So they want to walk together as much as possible.

The university which Zfy and Kevin study in can be modeled as a set of streets and junctions. Each street connects a pair of distinct junctions and can be walked in both directions. No two streets connect the same pair of junctions. Zfy and Kevin do not live together, and they do not live at the mine. There is at least one path from the mine to Zfy’s dormitory; the same occurs with Kevin’s dormitory.

Given information about the streets and junctions in the university, the locations of the mine, Zfy’s dormitory and Kevin’s dormitory, you must tell Zfy and Kevin the maximum distance that they can walk together without forcing them to walk more than the minimum distance from the mine to their respective dormitories. Zfy and Kevin also want to know how much each of them will walk alone.

Input

The first line contains an integer T, which is the number of test cases. The first line of each test case contains five integers J, B, C, N and S separated by single spaces. The value J is the number of junctions in the University (3 ≤ J ≤ 1000); each junction is identified by an integer number between 1 and J. The values B, C and N are the identifiers of the junctions where the mine, Zfy’s dormitory and Kevin’s are located, respectively (1 ≤ B, C, N ≤ J); these three junction numbers are different. The value S is the number of streets in the university (2 ≤ S ≤ 150000). Each of the next S lines contains the description of a street. Each street is described using three integers E1, E2 and L separated by single spaces, where E1 and E2 indicate two distinct junctions that are endpoints of the street (1 ≤ E1, E2 ≤ J), and L is the length of the street (1 ≤ L ≤ 104). You may assume that each street has a different pair of junctions, and that there always exist paths from junction B to junctions C and N.

Output

For each test case output a single line with three integers T, C and N separated by single spaces, where T is the maximum distance that Zfy and Kevin can walk together, C is the distance that Zfy walks alone, and N is the distance that Kevin walks alone.

Sample Input

2
5 3 2 1 6
3 4 10
4 5 10
5 1 3
5 2 4
1 3 23
2 3 24
8 1 7 8 8
1 2 1
2 4 1
2 3 1
4 5 1
3 5 1
5 6 1
6 8 1
6 7 1


Sample Output

20 4 3
4 1 1


题意:给定三个顶点u、v1、v2,从 u 到 v1和v2距离最小的前提下,公共路径最长。
分析:三次dijkstra(可以求出给定一个源点,到其他所有可达的顶点的最短路),分别是以 u 为顶点得d1[] ,以v1为顶点得 d2[],以v3为顶点得 d3[]。
在d1[v1]==d1[i]+d2[i] && d1[v2]==d1[i]+d3[i] 条件下求max(d1[i]);

View Code

#include<iostream>
#include<cstring>
#define N 1010
#define INF 10000000
using namespace std;

int map

,lowcost
,d
;
bool s
;

void dijkstra(int u,int n)
{
int i,j,k,min;
memset(s,false,sizeof(s));
for(i=1;i<=n;i++)
{
lowcost[i]=map[u][i];
}
s[u]=true;d[u]=0;
for(i=1;i<n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(min>lowcost[j]&&!s[j])
{
min=lowcost[j];k=j;
}
}
d[k]=min;s[k]=true;
for(j=1;j<=n;j++)
{
if(!s[j]&&lowcost[j]>map[k][j]+d[k])
lowcost[j]=map[k][j]+d[k];
}
}
}

int main()
{
int n,u,v,m,i,j,k,w;//求u->v的最短路 n个顶点从1到n,m条边
while(cin>>u>>v>>n>>m)
{
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=(i==j?0:INF);
for(k=1;k<=m;k++)
{
cin>>i>>j>>w;
map[i][j]=map[j][i]=w;
}
dijkstra(u,n);
cout<<d[v]<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: