Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions:7875 | Accepted: 2846 |
Description
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 Ai and Bi, with length Li (1 ≤ Li ≤ 1,000,000) units if used. The input data set never names any {Ai, Bi} 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.
Input
* Line 1: Three space-separated integers: N, P, and K
* Lines 2..P+1: Line i+1 contains the three space-separated integers: Ai, Bi, and Li
Output
* 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.
Sample Input
5 7 1 1 2 5 3 1 4 2 4 8 3 2 3 5 2 9 3 4 7 4 5 6
Sample Output
4
设一个解cost,若大于cost,则边权设为1,否则设为0。
求1到n的最短路d,若d<=K,则说明可行解小于等于cost
否则可行解大于cost。
还有我发现用spfa求解时间上很不稳定,有的题用queue
能过而stack超时,有的则相反。比如这题,开始用的stack
超时了,而用queue才94ms.
代码:
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int tol,d[1002],head[1002],N,P,K;
int a[10005],b[10005],c[10005];
bool mp[1002];
struct node
{
int to,cost,next;
} rode[20005];
queue<int>Q;
void add(int a1,int b1,int c1,int d1)
{
rode[tol].to=b1;
if(c1>d1) rode[tol].cost=1;
else rode[tol].cost=0;
rode[tol].next=head[a1];
head[a1]=tol++;
}
void addedge(int mid)
{
tol=0;
memset(head,-1,sizeof(head));
for(int i=0; i<P; i++)
{
add(a[i],b[i],c[i],mid);
add(b[i],a[i],c[i],mid);
}
}
int spfa()
{
memset(mp,0,sizeof(mp));
memset(d,INF,sizeof(d));
d[1]=0,mp[1]=1;
Q.push(1);
while(!Q.empty())
{
int v=Q.front();
Q.pop();
mp[v]=0;
for(int i=head[v]; i!=-1; i=rode[i].next)
{
node e=rode[i];
if(d[e.to]>d[v]+e.cost)
{
d[e.to]=d[v]+e.cost;
if(!mp[e.to])
{
Q.push(e.to);
mp[e.to]=1;
}
}
}
}
return d[N];
}
int main()
{
int i,j;
scanf("%d%d%d",&N,&P,&K);
int ma=0;
for(i=0; i<P; i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
ma=max(ma,c[i]);
}
if(N==1)
{
printf("0\n");
return 0;
}
int q=-1,r=ma;
while(r-q>1)
{
int mid=(r+q)/2;
addedge(mid);
int s=spfa();
if(s==INF)
{
r=-1;
break;
}
else if(s<=K)r=mid;
else q=mid;
}
printf("%d\n",r);
return 0;
}