[POJ - 2387] Til the Cows Come Home(最短路)

本文介绍了一个问题场景,即Bessie计划通过断开某个农场来破坏由N个农场组成的网络,使得每个剩余部分的农场数量不超过总数的一半。文章提供了一段C++代码示例,演示如何使用Dijkstra算法来找出所有符合条件的农场。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Link:http://poj.org/problem?id=2378

Description

After Farmer John realized that Bessie had installed a "tree-shaped" network among his N (1 <= N <= 10,000) barns at an incredible cost, he sued Bessie to mitigate his losses. 

Bessie, feeling vindictive, decided to sabotage Farmer John's network by cutting power to one of the barns (thereby disrupting all the connections involving that barn). When Bessie does this, it breaks the network into smaller pieces, each of which retains full connectivity within itself. In order to be as disruptive as possible, Bessie wants to make sure that each of these pieces connects together no more than half the barns on FJ. 

Please help Bessie determine all of the barns that would be suitable to disconnect.

Input

  • Line 1: A single integer, N. The barns are numbered 1..N.

  • Lines 2..N: Each line contains two integers X and Y and represents a connection between barns X and Y.

Output

  • Lines 1..?: Each line contains a single integer, the number (from 1..N) of a barn whose removal splits the network into pieces each having at most half the original number of barns. Output the barns in increasing numerical order. If there are no suitable barns, the output should be a single line containing the word “NONE”.

Sample Input

10
1 2
2 3
3 4
4 5
6 7
7 8
8 9
9 10
3 8

Sample Output

3
8

====还是很裸的最短路,注意一下输入的 t代表路数 ,n代表点数====


Code:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
int len[1010][1010];        //存相连边的长度 
int dis[1010];           //记录到起点的距离 
vector<int> edge[1010]; //存与之相连的边的点 
bool vis[1010];
struct Pair
{
    int fi,se; //fi存点,se存距离 
    bool friend operator < (Pair a,Pair b)
    {
        return a.se>b.se;  //从小到大排序,小的优先级高 
    }
}pr,ne;
void dijkstra()
{
    memset(dis,INF,sizeof(dis));
    memset(vis,false,sizeof(vis));
    dis[1]=0;
    pr.fi=1;
    pr.se=0;
    priority_queue<Pair> Q;
    Q.push(pr);
    while(!Q.empty())
    {
        pr=Q.top();
        Q.pop();
        if(vis[pr.fi])
            continue;
        vis[pr.fi]=true;
        for(int i=0;i<edge[pr.fi].size();i++)
        {
            ne.fi=edge[pr.fi][i];
            ne.se=pr.se+len[pr.fi][ne.fi];
            if(ne.se<dis[ne.fi])
            {
                dis[ne.fi]=ne.se;
                Q.push(ne);
            }
        }
    }
}
int main()
{
    int n,t,c,i,a,b;
    memset(len,-1,sizeof(len));
    scanf("%d%d",&t,&n);
    for(i=1;i<=t;i++)
    {
        scanf("%d%d%d",&a,&b,&c);
        edge[a].push_back(b);
        edge[b].push_back(a);   //双向存图 
        if(len[a][b]==-1)
            len[a][b]=len[b][a]=c;
        else
            len[a][b]=len[b][a]=min(c,len[a][b]);
    }
    dijkstra();
    printf("%d\n",dis[n]);
return 0;   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值