标签:style blog class code java c
题目描述
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncooperative, so he needs to pay for some of the cables required to connect his farm to the phone system. There are N (1 <= N <= 1,000) forlorn telephone poles conveniently numbered 1..N that are scattered around Farmer John‘s property; no cables connect any them. A total of P (1 <= P <= 10,000) pairs of poles can be connected by a cable; the rest are too far apart. The i-th cable can connect the two distinct poles A_i and B_i, with length L_i (1 <= L_i <= 1,000,000) units if used. The input data set never names any {A_i,B_i} pair more than once. Pole 1 is already connected to the phone system, and pole N is at the farm. Poles 1 and N need to be connected by a path of cables; the rest of the poles might be used or might not be used. As it turns out, the phone company is willing to provide Farmer John with K (0 <= K < N) lengths of cable for free. Beyond that he will have to pay a price equal to the length of the longest remaining cable he requires (each pair of poles is connected with a separate cable), or 0 if he does not need any additional cables. Determine the minimum amount that Farmer John must pay.
输入
* Line 1: Three space-separated integers: N, P, and K * Lines 2..P+1: Line i+1 contains the three space-separated integers: A_i, B_i, and L_i
输出
* Line 1: A single integer, the minimum amount Farmer John can pay. If it is impossible to connect the farm to the phone company, print -1.
样例输入
5 7 1 1 2 5 3 1 4 2 4 8 3 2 3 5 2 9 3 4 7 4 5 6
样例输出
4
这个题目的意思就不用解释了,本来我的英语就不好,用二分加最短路来判断,若二分当前值为ans,则大于ans的边的长度为1,小于等于得都设为0,做一次最短路,若最短路小于等于k,表明可行。还有我想说的是,为什么网上总是说dijkstra算法慢呢,他是可以优化的,用优先队列和邻接表来优化,它的速度也是很快的,我的程序也是在108ms通过的
#include<iostream> #include<cstdio> #include<queue> #include<utility> #include<vector> #include<cstring> #include<algorithm> using namespace std; const int inf=10000000; const int MAX=10000+10; typedef pair<int,int>pii; priority_queue<pii,vector<pii>,greater<pii> >q; struct node { int id,cost; }; vector<node> map[MAX]; int dist[MAX],n,m,k; int u[MAX],v[MAX],c[MAX]; bool vis[MAX]; void dijkstra() { for(int i=0;i<=n;i++) dist[i]=(i==1? 0 : 199999999); memset(vis,0,sizeof(vis)); q.push(make_pair(dist[1],1)); while(!q.empty()) { pii u=q.top(); q.pop(); int x=u.second; if(!vis[x]) { vis[x]=true; for (int i=0;i<map[x].size();i++) { if(dist[map[x][i].id]>dist[x]+map[x][i].cost) { dist[map[x][i].id]=dist[x]+map[x][i].cost; q.push(make_pair(dist[map[x][i].id],map[x][i].id)); } } } } } void init() { for (int i=0;i<=n;i++) map[i].clear(); } void built1() { init(); for (int i=1;i<=m;i++) { node temp; temp.id=v[i]; temp.cost=1; map[u[i]].push_back(temp); temp.id=u[i]; map[v[i]].push_back(temp); } } void built(int mm) { init(); for (int i=1;i<=m;i++) { if (c[i]>mm) { node temp; temp.id=v[i]; temp.cost=1; map[u[i]].push_back(temp); temp.id=u[i]; map[v[i]].push_back(temp); } else { node temp; temp.id=v[i]; temp.cost=0; map[u[i]].push_back(temp); temp.id=u[i]; map[v[i]].push_back(temp); } } } int find(int x,int y) { while(x<y) { int mid=x+(y-x)/2; built(mid); dijkstra(); if (dist[n]<=k) y=mid; else x=mid+1; } return x; } int main() { int maxx; while (scanf("%d%d%d",&n,&m,&k)!=EOF) { maxx=-1; for (int i=1;i<=m;i++) { scanf("%d%d%d",&u[i],&v[i],&c[i]); maxx=max(maxx,c[i]); } built1(); dijkstra(); if (dist[n]<199999999) printf("%d\n",find(0,maxx)); else printf("-1\n"); } return 0; }
hust 1039 Telephone Lines,布布扣,bubuko.com
标签:style blog class code java c
原文地址:http://www.cnblogs.com/chensunrise/p/3729853.html