Silver Cow Party

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 ≤ XN). 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.

思路:正着建边跑djs倒着建边跑dijis然后数组存一下所有的值求牛正跑倒跑的和最大

代码:


#include <iostream>

#include <cstdio>

#include <cstring>

#include <queue>

using namespace std;

const int MAXN = 1e5+5;

typedef long long LL;

const LL INF = 0x3f3f3f3f3f3f3f3f;

 

struct Edge

{

    int from, to; LL dist;       //起点,终点,距离

    Edge(int from, int to, LL dist):from(from), to(to), dist(dist) {}

};

 

struct Dijkstra

{

    int n, m;                 //结点数,边数(包括反向弧)

    vector<Edge> edges;       //边表。edges[e]和edges[e^1]互为反向弧

    vector<int> G[MAXN];      //邻接表,G[i][j]表示结点i的第j条边在edges数组中的序号

    int vis[MAXN];            //标记数组

    LL d[MAXN];              //s到各个点的最短路

    int p[MAXN];              //上一条弧

 

    void init(int n)

    {

        this->n = n;

        edges.clear();

        for (int i = 0; i <= n; i++) G[i].clear();

    }

 

    void AddEdge(int from, int to, int dist)

    {

        edges.push_back(Edge(from, to, dist));

        m = edges.size();

        G[from].push_back(m - 1);

    }

 

    struct HeapNode

    {

        int from; LL dist;

        bool operator < (const HeapNode& rhs) const

        {

            return rhs.dist < dist;

        }

        HeapNode(int u, LL w): from(u), dist(w) {}

    };

 

    void dijkstra(int s)

    {

        priority_queue<HeapNode> Q;

        for (int i = 0; i <= n; i++) d[i] = INF;

        memset(vis, 0, sizeof(vis));

        d[s] = 0;

        Q.push(HeapNode(s, 0));

        while (!Q.empty())

        {

            HeapNode x = Q.top(); Q.pop();

            int u = x.from;

            if (vis[u]) continue;

            vis[u] = true;

            for (int i = 0; i < G[u].size(); i++)

            {

                Edge& e = edges[G[u][i]];

                if (d[e.to] > d[u] + e.dist)

                {

                    d[e.to] = d[u] + e.dist;

                    p[e.to] = G[u][i];

                    Q.push(HeapNode(e.to, d[e.to]));

                }

            }

        }

    }

}gao;
struct node{
	int x,y,c;
}pp[19000];
int ans[10009];
int main()
{
	long long n,m,x;
	while(~scanf("%lld%lld%lld",&n,&m,&x))
	{
		memset(pp,0,sizeof(pp));
		long long a,b,c;
		gao.init(n+1);
		for(long long i = 0;i< m;++i)
		{
			scanf("%lld%lld%lld",&pp[i].x,&pp[i].y,&pp[i].c);
			gao.AddEdge(pp[i].x,pp[i].y,pp[i].c);	
		}	
		gao.dijkstra(x);
		for(long long i=1;i<=n;++i)
		{
			ans[i] = gao.d[i];
			
		}
		gao.init(n+1);
		for(long long i=0;i<m;++i)
		{
			gao.AddEdge(pp[i].y,pp[i].x,pp[i].c);
		}
		gao.dijkstra(x);
		long long max1 = -INF;
		for(long long i = 1; i <= n; i++)
		{
			max1 = max(max1,gao.d[i]+ans[i]);
		}
		printf("%lld\n",max1);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值