# 1496 架设电话线

2021/8/21 12:31:51 浏览：

## 1496 架设电话线

``````#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
const int N = 1e3+10,M = 2e5+10,INF = 0x3f3f3f3f;//记录长度
typedef pair<int,int> P;
int n,m,k,cnt;
{
ver[++cnt] = y;
edge[cnt] = w;
}
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1,x,y,w;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
for(int j=1;j<=k;j++)
{
}
}
}
void dj()
{
priority_queue<P> q;
for(int i=0;i<=M;i++) dis[i] = INF;
q.push(make_pair(0,1)); dis[1] = 0;//初始化dis数组
while(q.size())
{
int u = q.top().second;q.pop();//取出队首进行操作
if(vis[u]) continue;
vis[u] = 1;//未标记标记
{
int v = ver[i],w = edge[i];
if(dis[v] > max(dis[u],w))
{
dis[v] = max(dis[u],w);//更新dis数组
if(!vis[v]) q.push(make_pair(-dis[v],v));//进行标记，存反距离
}
}
}
}
void solve()
{
if(dis[k*n+n]!=INF) printf("%d\n",dis[k*n+n]);
else printf("-1\n");
}
int main()
{
dj();
solve();
return 0;
}
``````

``````#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+10,INF = 0x3f3f3f3f;
typedef pair<int,int> P;
int l=0,r=1e6,mid;
int n,m,k,cnt;
{
ver[++cnt] = y;
edge[cnt] = w;
}
{
int x,y,w;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
}
}
bool check(int x)
{
for(int i=0;i<N;i++)
dis[i] = INF,vis[i]=0;
priority_queue<P> q;
q.push(make_pair(0,1));dis[1] = 0;
while(q.size())
{
int u =q.top().second;q.pop();
if(vis[u]) continue;
vis[u] = 1;
{
int v = ver[i],w = edge[i];
int nd = dis[u] + (w>x?1:0);
if(dis[v] > nd){
dis[v] = nd;
if(!vis[v])
q.push(make_pair(-dis[v],v));
}
}
}
return (dis[n] <= k);//判断是否满足最短路
}
void solve()
{
while(l<=r)
{
mid = ((r+l)>>1);
if(check(mid))
r = mid-1;
else l = mid+1;//二分寻找最短路径
}
if(r==1e6) puts("-1");
else printf("%d\n",l);
}
int main()
{
solve();
return 0;
}
``````