首页 > 其他分享 >1017 [USACO 2007 Ope B]Bronze Cow Party dij 板子题

1017 [USACO 2007 Ope B]Bronze Cow Party dij 板子题

时间:2022-08-19 03:55:06浏览次数:79  
标签:dist dij Cow int USACO farm party ver

链接:https://ac.nowcoder.com/acm/contest/26077/1017
来源:牛客网

题目描述

One cow from each of N farms (1 <= N <= 1000) conveniently numbered 1..N is attending the big cow party to be held at farm #X (1 <= X <= N). Each of M (1 <= M <= 100,000) bidirectional roads connects one farm to another. It is always possible to travel from one farm to another on the roads. Traversing road i requires Ti (1 <= Ti <= 100) units of time. One or more pairs of farms might be directly connected by more than one road.
After all the cows gathered at farm #X, they realized that every single cow left her party favors back at the farm. They decided to suspend the party and send all the cows back to get the party favors. The cows all travel optimal routes to their home farm and back to the party. What is the minimum number of time units the party must be suspended?

输入描述:

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, respectively: Ai, Bi, and Ti. The described road connects Ai and Bi and requires Ti time units to traverse.

输出描述:

Line 1: One integer: the minimum amount of time the party must be suspended.
示例1

输入

复制
4 8 2
1 2 7
1 3 8
1 4 4
2 1 3
2 3 1
3 1 2
3 4 6
4 2 2

输出

复制
6

说明

Four cows; eight roads; party at farm 2.
Direct paths connects farm 2 to each of the other farms (to farm 1: 7 and 3; to farm 3: 1; to farm 4: 2). The longest path is length 3, so the round-trip time is 6.

 

分析

题意:求从x点向其他所有点往返的最长距离

板子。。

//-------------------------代码----------------------------

//#define int ll
const int N = 2e5+10;
int n,m;
V<pii> e[N];
bool vis[N];
int dist[N];
void dij(int st) {
    priority_queue<pii,V<pii>,greater<pii>> q;
    ms(dist,0x3f);ms(vis,0);
    q.push({0,st});
    dist[st] = 0;
    while(q.size()) {
        auto tmp = q.top();q.pop();
        int ver = tmp.y;
        if(vis[ver]) continue;
        vis[ver] = 1;
        for(auto it:e[ver]) {
            int j = it.first;
            if(dist[j] > dist[ver] + it.second) {
                dist[j] = dist[ver] + it.second;
                q.push({dist[j],j});
            }
        }
    }
}
int c,p,a1,a2,a3,x;
void solve()
{
    cin>>n>>m>>x;
    fo(i,1,m) {int u,v,w;cin>>u>>v>>w;e[u].pb({v,w});e[v].pb({u,w});
    }
    
    int ans = 0;
    dij(x);
//     cout<<2122219134/2<<endl;
    fo(i,1,n) {
        if(dist[i] != 1061109567);
        ans = max(ans,dist[i]);
//         db(dist[i]);
    }
    cout<<ans*2<<endl;
}
void main_init() {}
signed main(){
    AC();clapping();TLE;
    cout<<fixed<<setprecision(12);
    main_init();
//  while(cin>>n,n)
//  while(cin>>n>>m,n,m)
//    int t;cin>>t;while(t -- )
    solve();
//    {solve(); }
    return 0;
}

/*样例区


*/

//------------------------------------------------------------

 

标签:dist,dij,Cow,int,USACO,farm,party,ver
From: https://www.cnblogs.com/er007/p/16600697.html

相关文章