P1948 [USACO08JAN] Telephone Lines spfa telephone line answer bipartite

  

Years later, Ben Ben grew up and became a telephone line layout division. Since the earthquake in a city phone line all the damage, simple-minded, responsible person in charge of the city to the epicenter. Around the city distribution N (1 <= N <= 1000) according to sequence number 1 ...... n discarded telephone poles, no telephone line is connected between any two poles, a total of p (1 <= p < = 10,000) can be pulled on the telephone line a telephone pole. Other makes can not be connected due to the earthquake.

Two endpoints of the i-th poles are ai, bi, their distance li (1 <= li <= 1000000). Data only once for each pair (ai, bi). No. 1 telephone poles have access to the national telephone network, telephone lines throughout the city are all connected to the N number of telephone poles. In other words, just simple-minded mission is to find a No. 1 and No. N poles to link the path to the rest of the telephone pole is not necessarily connected to the telephone network.

Telecommunications company decided to support the disaster areas to free local connection k designated by the simple-minded, telephone poles, in addition to those phone lines, they need to pay for the total cost of which depends on the length of the longest telephone lines (per telephone line connect only one pair of telephone poles). If you need to connect a telephone pole does not exceed k right, then the expenditure is zero.

Please you do the math, the epicenter of the city telephone line guide least how much money to spend on the phone line?

Input and output formats

Input formats:

 

The first line of the input file contains three numbers n, p, k;

The second p + 1 row to row, for each row are three integers ai, bi, li.

 

Output formats:

 

An integer representing the minimum expenditure of the project, if it is impossible to complete the output -1.

 

Sample input and output

Input Sample # 1:  Copy
5 7 1
1 2 5
3 1 4
2 4 8
3 2 3
5 2 9
3 4 7
4 5 6
Output Sample # 1:  Copy
4 

Find (route 1-n) k + 1 th long way and minimize
road less than two score was greater than 0 to 1
#include<bits/stdc++.h>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define LL long long
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define inf 2147483647
#define N 500000+50
int dis[N],am [N] head [N], n, m, s, pos, k, L, R, years;
struct Edge
{
    int next,v,to;
}edge[N];
void addedge(int a,int b,int c)
{
    edge[++pos]=Edge{head[a],c,b};
    head[a]=pos;
}
bool spfa(int x)
{
    queue<int>q;
    rep(i,1,n)
    dis[i]=inf,vis[i]=0;

    q.push(1);
    dis[1]=0;
    vis[1]=1;
    while(!q.empty())
    {
        int u=q.front();q.pop();
        vis[u]=0;
        for(int i=head[u];i;i=edge[i].next)
        {
            int v=edge[i].to;
            int w;
            if(edge[i].v<=x)w=0;
            else w=1;

            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    return dis[n]<=k;
}
int main()
{
    cin>>n>>m>>k;
    rep(i,1,m)
    {
        int a,b,c;
        RIII(a,b,c);
        addedge(a,b,c);//有向边
        addedge(b,a,c);
        R=max(R,c);
    }
    ans=-1;
    while(L<=R)
    {
        int mid=(L+R)>>1;
        if(spfa(mid))R=mid-1,ans=mid;
        else L=mid+1;
    }
    cout<<ans;
    return 0;
}
View Code

 





Guess you like

Origin www.cnblogs.com/bxd123/p/10959286.html