#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
using namespace std;
const int maxn = 100009;
const int maxxn = 1009;
#define inf 0x3f3f3f
queue<int > q;
int tot = 0;
int n,m,k;
int maxx = -100;
int minn = 10000000;
int mid;
int l,r;
int head[maxxn];
int d[maxxn];
int v[maxxn];
struct node {
int fr,ver,value,Next;
};
node edge[maxn];
inline bool spfa () {
memset(d,inf,sizeof(d));
memset(v,0,sizeof(v));
d[1] = 0;
q.push(1);
v[1] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
v[x] = 0;
for (int i = head[x];i;i = edge[i].Next) {
int y = edge[i].ver;
int z = edge[i].value;
if (z > mid) z = 1;
else z = 0;
if (d[y] > d[x] + z) {
d[y] = d[x] + z;
if (v[y] == 0) {
q.push(y);
v[y] = 1;
}
}
}
}
if (d[n] <= k) return 1;
else return 0;
}
inline void add (int x,int y,int z) {
edge[++tot].ver = y; edge[tot].fr = x; edge[tot].value = z; edge[tot].Next = head[x]; head[x] = tot;
}
inline int Read () {
int xx = 0;
int ff = 1;
char ch = getchar();
while (ch < '0'||ch > '9') {
if (ch == '-') ff = -1;
ch = getchar();
}
while (ch >= '0'&&ch <= '9') {
xx = (xx << 1) + (xx << 3) + ch - '0';
ch = getchar();
}
return xx * ff;
}
int main () {
n = Read(); m = Read(); k = Read();
for (int i = 1;i <= m;i++) {
int a = Read(); int b = Read(); int c = Read();
maxx = max(c,maxx);
add(a,b,c); add(b,a,c);
}
l = 0;
r = maxx;
int ans = -1;
while (l <= r) {
mid = (l + r) / 2;
if (spfa() == 1) {
ans = mid,r = mid - 1;
}
else {
l = mid + 1;
}
}
printf ("%d\n",ans);
return 0;
}
但是下面的这个就挂了
二分是有一定的使用技巧的
二分的最后一个mid值不一定合法,(因为即使不合法,l与r也会更新,而只要满足l>=r就会跳出)所以一定要记录最后一个合法的mid
#include<algorithm>
#include<bitset>
#include<cctype>
#include<cerrno>
#include<clocale>
#include<cmath>
#include<complex>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<utility>
#include<vector>
#include<cwchar>
#include<cwctype>
using namespace std;
const int maxn = 100009;
const int maxxn = 1009;
#define inf 0x3f3f3f
queue<int > q;
int tot = 0;
int n,m,k;
int maxx = -100;
int minn = 10000000;
int mid;
int l,r;
int head[maxxn];
int d[maxxn];
int v[maxxn];
struct node {
int fr,ver,value,Next;
};
node edge[maxn];
inline bool spfa () {
memset(d,inf,sizeof(d));
memset(v,0,sizeof(v));
d[1] = 0;
q.push(1);
v[1] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
v[x] = 0;
for (int i = head[x];i;i = edge[i].Next) {
int y = edge[i].ver;
int z = edge[i].value;
if (z > mid) z = 1;
else z = 0;
if (d[y] > d[x] + z) {
d[y] = d[x] + z;
if (v[y] == 0) {
q.push(y);
v[y] = 1;
}
}
}
}
if (d[n] <= k) return 1;
else return 0;
}
inline void add (int x,int y,int z) {
edge[++tot].ver = y; edge[tot].fr = x; edge[tot].value = z; edge[tot].Next = head[x]; head[x] = tot;
}
inline int Read () {
int xx = 0;
int ff = 1;
char ch = getchar();
while (ch < '0'||ch > '9') {
if (ch == '-') ff = -1;
ch = getchar();
}
while (ch >= '0'&&ch <= '9') {
xx = (xx << 1) + (xx << 3) + ch - '0';
ch = getchar();
}
return xx * ff;
}
int main () {
n = Read(); m = Read(); k = Read();
for (int i = 1;i <= m;i++) {
int a = Read(); int b = Read(); int c = Read();
maxx = max(c,maxx);
add(a,b,c); add(b,a,c);
}
l = 0;
r = maxx;
while (l <= r) {
mid = (l + r) / 2;
if (spfa() == 1) {
r = mid - 1;
}
else {
l = mid + 1;
}
}
for (int i = 1;i <= tot;i++) {
if (edge[i].value >= mid && edge[i].value <= minn) minn = edge[i].value;
}
printf ("%d\n",minn==10000000?-1:minn);
return 0;
}