POJ-3268-Silver Cow Party(迪杰斯特拉 多点到star和star到多点)

来源:互联网 发布:mac新建的文件夹在哪看 编辑:程序博客网 时间:2024/05/17 09:22

D - Silver Cow Party
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

POJ 3268
Appoint description:
Description
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.

题意:首行给出N,M,X代表有N个点,M条边,接着M行每行u,v,w代表u到v权值为w,这是个有向图,求出各点到X加上X到各点最小权值中的最大值。

思路:迪杰斯特拉的变种,dis[i]=map[star][i]改成dis[i]=map[i][star]就是第二种情况
判断时改为dis[j]>dis[point]+map[j][point]?dis[j]=dis[point]+map[j][point]即可

代码

#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<math.h>#include<queue>#include<iomanip>using namespace std;//有向图各点到star和star到各点最短路const int maxn=1005;const int INF=0x3f3f3f3f;int map[maxn][maxn];//有向图int dis_to[maxn];//star到各点最短距离int dis_from[maxn];//各点到star最短距离bool vis_to[maxn];bool vis_from[maxn];int N;//图大小void Dijkstra(int star){    for(int i=1; i<=N; i++)    {        dis_to[i]=map[star][i];//star到各点        dis_from[i]=map[i][star];//各点到star        vis_from[i]=vis_to[i]=0;    }    vis_from[star]=vis_to[star]=1;    for(int i=1; i<N; i++) //star到各点最短路    {        int minn=INF;        int point;        for(int j=1; j<=N; j++)            if(vis_to[j]==0&&dis_to[j]<minn)            {                minn=dis_to[j];                point=j;            }        if(minn==INF)            break;        vis_to[point]=1;        for(int j=1; j<=N; j++)            if(vis_to[j]==0&&dis_to[j]>dis_to[point]+map[point][j])                dis_to[j]=dis_to[point]+map[point][j];    }    for(int i=1; i<N; i++) //各点到star最短路    {        int minn=INF;        int point;        for(int j=1; j<=N; j++)            if(vis_from[j]==0&&dis_from[j]<minn)            {                minn=dis_from[j];                point=j;            }        if(minn==INF)            break;        vis_from[point]=1;        for(int j=1; j<=N; j++)            if(vis_from[j]==0&&dis_from[j]>dis_from[point]+map[j][point])                dis_from[j]=dis_from[point]+map[j][point];    }}int main(){    int M,X;    scanf("%d%d%d",&N,&M,&X);    for(int i=1; i<=N; i++)        for(int j=1; j<=N; j++)            i==j?map[i][j]=0:map[i][j]=INF;    while(M--)    {        int u,v,w;        scanf("%d%d%d",&u,&v,&w);        if(map[u][v]>w)            map[u][v]=w;//有向图    }    Dijkstra(X);    int max_num=-1;    for(int i=1; i<=N; i++)        if(dis_to[i]+dis_from[i]>max_num)            max_num=dis_to[i]+dis_from[i];    printf("%d\n",max_num);    return 0;}
0 0