poj-3628 Silver Cow Party

题目链接:http://poj.org/problem?id=3268

所有的牛去一个牛的家里参加聚会,所给的路径是单向的,问往返的所有牛的路径长度中最长的是多少?

解法:往返时候翻转矩阵,再求一遍最短路径,两次相加后进行比较。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

const int MAXV = 4010;
const int inf = 10000000;

int map[MAXV][MAXV];//路径长度
int d[MAXV],re_d[MAXV];//距离
bool vis[MAXV];
int n,m,x;

void dijkstra(int s)
{
	for(int i=1;i<=n;i++)
    {
		vis[i]=0;
		d[i]=map[s][i];
	}
    d[s] = 0;
	while (1)
    {
		int min=inf,v = -1;
		for(int i=1;i<=n;i++)
			if(!vis[i] && d[i]<min)
			{
				v=i;
				min=d[i];
			}
        if(v == -1)
            break;
		vis[v]=1;
		for(int i=1;i<=n;i++)
			if(!vis[i] && d[i] > d[v] + map[v][i])
				d[i]=map[v][i]+d[v];
	}
}
int main()
{
	int i,j,a,b,c;
	while(scanf("%d%d%d",&n,&m,&x) != EOF)
    {
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                if(i==j)
                    map[i][i]=0;
                else
                    map[i][j] = inf;
            }
            for(i=1;i<=m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                if(map[a][b]>c)
                    map[a][b]=c;
            }
		dijkstra(x);
		for(int i=1;i<=n;i++)
            re_d[i] = d[i];
		for(int i=1;i<=n;i++)
            for(int j=1;j<i;j++)
        {
            int t = map[i][j] ;
            map[i][j]= map[j][i];
            map[j][i] = t;
        }
        dijkstra(x);
        for(int i=1;i<=n;i++)
            d[i]+=re_d[i];
        int ans = -1;
        for(int i=1;i<=n;i++)
            ans = max(ans,d[i]);
		printf("%d\n",ans);
	}
	return 0;
}
时间: 2025-01-04 05:58:24

poj-3628 Silver Cow Party的相关文章

POJ 3268 Silver Cow Party(SPFA)

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 re

图论 ---- spfa + 链式向前星 ---- poj 3268 : Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12674   Accepted: 5651 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 

POJ 3268 Silver Cow Party 最短路—dijkstra算法的优化。

POJ 3268 Silver Cow Party 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

poj 3268 Silver Cow Party(最短路)

poj 3268 Silver Cow Party 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

POJ 3268 Silver Cow Party dijkstra单源最短路

裸dijkstra 思路:以x为源点,求到其他点的最短路,之后把邻接矩阵转置,再求一次x源 点的最短路,这样就一次是来的,一次是走的,相加迭代最大值即可 代码: /* poj 3268 8108K 47MS */ #include<cstdio> #include<iostream> #define MAXN 1005 #define MAX_INT 2147483647 using namespace std; int gra_in[MAXN][MAXN],gra_out[MAX

poj 3268 Silver Cow Party , spfa , dijkstra

点击打开链接 两次求最短路(第二次把边反向求) 1.spfa //poj 3268 Silver Cow Party //SPFA #include <cstdio> #include <cstring> #include <queue> using namespace std; const int M = 100000 + 100; const int N = 1000 + 100; const int inf = 1<<25; struct Graph

POJ 3268 Silver Cow Party (来回最短路 SPFA)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14384   Accepted: 6490 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 ≤

POJ 3268 Silver Cow Party

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20274   Accepted: 9278 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 

poj 3268 Silver Cow Party(dijkstra||SPFA)(中等)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14900   Accepted: 6746 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 

poj 3268 Silver Cow Party (最短路)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12913   Accepted: 5778 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