Here
典中之典!!
解题思路
- 可选k条边代价为0
- 如何决策?
- 将到当前位置选择了几条代价为0的边放入状态,即
- 若当前状态选的边数小于,则可以进行决策,是否选择当前边,
- 若选,则,否则
- 若当前状态选的边数等于,则只能
- 考虑分层图,建k层
- (实际上就是将上一种方法中多一维记录来区别状态),改为用点的标号区分)
- 对于每条边,向层连一条相同但代价为0的边
- 同层连代价为的边
- 最终输出第k层的
for(int i=1;i<=m;++i){
int x=read(),y=read(),w=read();
for(int j=0;j<=k;++j){
Lian(x+j*n,y+j*n,w);
Lian(y+j*n,x+j*n,w);
if(j==k)continue;
Lian(x+j*n,y+(j+1)*n,0);
Lian(y+j*n,x+(j+1)*n,0);
}
}
- 由于代价具有单调性(代价低的,花费更高代价一定也行)
- 二分答案
- 对于每条边的边权有小到大排序去重 ,将边权视为答案进行
- 时,对大于当前所选答案的边,视作代价为1的边,小于的视作代价为0
- 判断最终,即为可能解,接着往小进行尝试
- 否则,往大进行尝试
//二分答案
import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;
//implements Runnable
public class Main{
static long md=(long)20020219;
static long Linf=Long.MAX_VALUE/2;
static int inf=Integer.MAX_VALUE/2;
static int M=10000;
static int N=1000;
static int n=0;
static int m=0;
static int k=0;
static
class Edge{
int fr,to,val,nxt;
public Edge(int u,int v,int w) {
fr=u;
to=v;
val=w;
}
}
static Edge[] e=new Edge[2*M+1];
static int[] head=new int[N+1];
static int cnt=0;
static void addEdge(int fr,int to,int val) {
cnt++;
e[cnt]=new Edge(fr, to, val);
e[cnt].nxt=head[fr];
head[fr]=cnt;
}
static
class Node{
int x;
int y;
public Node(int u,int v) {
x=u;
y=v;
}
}
static boolean Dij(int s,int lim) {
int[] dis=new int[N+1];
Arrays.fill(dis, inf);
boolean[] vis=new boolean[N+1];
PriorityQueue<Node> q=new PriorityQueue<Node>((o1,o2)->{
return o1.y-o2.y;
});
q.add(new Node(s, 0));
dis[s]=0;
while(!q.isEmpty()) {
Node now=q.peek();
q.poll();
int x=now.x;
if(vis[x])continue;
vis[x]=true;
for(int i=head[x];i>0;i=e[i].nxt) {
int v=e[i].to;
if(vis[v])continue;
int w=e[i].val;
if(w>lim) {
if(dis[x]+1<dis[v]) {
dis[v]=dis[x]+1;
q.add(new Node(v, dis[v]));
}
}else {
if(dis[x]<dis[v]) {
dis[v]=dis[x];
q.add(new Node(v, dis[v]));
}
}
}
}
if(dis[n]<=k)return true;
else return false;
}
public static void main(String[] args) throws Exception{
AReader input=new AReader();
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
n=input.nextInt();
m=input.nextInt();
k=input.nextInt();
int[] a=new int[m+1];
int aa=0;
int[] b=new int[m+1];
int bb=0;
for(int i=1;i<=m;++i) {
int u=input.nextInt();
int v=input.nextInt();
int w=input.nextInt();
addEdge(u, v, w);
addEdge(v, u, w);
aa++;
a[aa]=w;
}
Arrays.sort(a,1,aa+1);
for(int i=1;i<=aa;++i) {
if(a[i]!=a[i-1]) {
bb++;
b[bb]=a[i];
}
}
int l=1,r=bb;
int ans=-1;
while(l<=r) {
int mid=(l+r)>>1;
if(Dij(1, b[mid])) {
ans=b[mid];
r=mid-1;
}else l=mid+1;
}
out.print(ans);
out.flush();
out.close();
}
// public static final void main(String[] args) throws Exception {
// new Thread(null, new Main(), "线程名字", 1 << 27).start();
// }
// @Override
// public void run() {
// try {
// //原本main函数的内容
// solve();
//
// } catch (Exception e) {
// }
// }
// static
// class Node{
// int x;
// int y;
// public Node(int u,int v) {
// x=u;
// y=v;
// }
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
// Node may = (Node) o;
// return x == may.x && y==may.y;
// }
//
// @Override
// public int hashCode() {
// return Objects.hash(x, y);
// }
// }
static
class AReader{
BufferedReader bf;
StringTokenizer st;
BufferedWriter bw;
public AReader(){
bf=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
bw=new BufferedWriter(new OutputStreamWriter(System.out));
}
public String nextLine() throws IOException{
return bf.readLine();
}
public String next() throws IOException{
while(!st.hasMoreTokens()){
st=new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
public char nextChar() throws IOException{
//确定下一个token只有一个字符的时候再用
return next().charAt(0);
}
public int nextInt() throws IOException{
return Integer.parseInt(next());
}
public long nextLong() throws IOException{
return Long.parseLong(next());
}
public double nextDouble() throws IOException{
return Double.parseDouble(next());
}
public float nextFloat() throws IOException{
return Float.parseFloat(next());
}
public byte nextByte() throws IOException{
return Byte.parseByte(next());
}
public short nextShort() throws IOException{
return Short.parseShort(next());
}
public BigInteger nextBigInteger() throws IOException{
return new BigInteger(next());
}
public void println() throws IOException {
bw.newLine();
}
public void println(int[] arr) throws IOException{
for (int value : arr) {
bw.write(value + " ");
}
println();
}
public void println(int l, int r, int[] arr) throws IOException{
for (int i = l; i <= r; i ++) {
bw.write(arr[i] + " ");
}
println();
}
public void println(int a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(int a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(String a) throws IOException{
bw.write(a);
bw.newLine();
}
public void print(String a) throws IOException{
bw.write(a);
}
public void println(long a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(long a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(double a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
public void print(double a) throws IOException{
bw.write(String.valueOf(a));
}
public void print(char a) throws IOException{
bw.write(String.valueOf(a));
}
public void println(char a) throws IOException{
bw.write(String.valueOf(a));
bw.newLine();
}
}
}