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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/JomoDimple_/article/details/76786314

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;   
}

没有更多推荐了,返回首页