1,acwing1135.新年好 ;2,acwing 340. 通信线路;
1,新年好;
思路:先预处理以1,a,b,c,d,e为起点到所有点的单源最短路;
dfs所有拜访顺序,共(5!)种拜访顺序,找出最短的一种;
注意memset(dist,0x3f N*4)和memset(dist ,0x3f sizeof N*4)是有区别的,所以宏定义最好能区分这俩;
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i)
#define rep2(i,a,n) for( int i=a;i<=n;++i)
#define per1(i,n,a) for( int i=n;i>a;i--)
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define mset(a,i,b) memset((a),(i),sizeof (b))
#define mcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long LL;
typedef double dob;
const int N=2e5+10;
int e[N],ne[N],w[N],h[N],idx;
void add(int a,int b,int c)
{
w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
int q[N],dist[6][N],source[6],st[N];
int n,m;
void dijkstra(int start,int dist[])
{
memset(dist,0x3f,N*4);
dist[start]=0;
mset(st,0,st);
pro_q<PII,vector<PII>,greater<PII>>heap;
heap.push({0,start});
while(heap.size())
{
auto t=heap.top();
heap.pop();
int ver=t.second;
if(st[ver])continue;
st[ver]=1;
for(int i=h[ver];~i;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[ver]+w[i])
{
dist[j]=dist[ver]+w[i];
heap.push({dist[j],j});
}
}
}
}
int dfs(int u,int start,int distance)
{
if(u>5)return distance;
int res=INF;
rep2(i,1,5)
{
if(!st[i])
{
int next=source[i];
st[i]=1;
res=min(res,dfs(u+1,i,distance+dist[start][next]));
st[i]=0;
}
}
return res;
}
signed main()
{
quick_cin();
cin>>n>>m;
source[0]=1;
mset(h,-1,h);
rep2(i,1,5)cin>>source[i];
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
rep1(i,0,6)dijkstra(source[i],dist[i]);
mset(st,0,st);
cout<<dfs(1,0,0);
return 0;
}
2,通信线路;
题意:找出1~n号的路径中,第k+1大的路径权值最小;所以不一定就是在最短路里找了;
一种解法是二分,为什么可以二分呢,
二分找的是一个分界点,使得右边满足一个性质,左边满足一个性质;
边权定义:长度大于x的权值为1,小于x的为0,所以走到n的路径值就代表经过几条大于x的路径;
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for( int i=a;i<n;++i)
#define rep2(i,a,n) for( int i=a;i<=n;++i)
#define per1(i,n,a) for( int i=n;i>a;i--)
#define per2(i,n,a) for( int i=n;i>=a;i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define mset(a,i,b) memset((a),(i),sizeof (b))
#define mcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
using namespace std;
typedef pair<int,int> PII;
typedef pair<long long,long long>PLL;
typedef pair<int,PII> PIII;
typedef long long LL;
typedef double dob;
const int N=1e5+10;
int n,m,k;
int e[N],ne[N],w[N],h[N],idx;
int dist[N];
deque<int>q;
int st[N];
void add(int a,int b,int c)
{
w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
bool check(int bound)
{
mset(dist,0x3f,dist);
mset(st,0,st);
q.push_back(1);
dist[1]=0;
while(q.size())
{
int t=q.front();
q.pop_front();
if(st[t])continue;
st[t]=1;
for(int i=h[t];~i;i=ne[i])
{
int j=e[i],v=w[i]>bound;
if(dist[j]>dist[t]+v)
{
dist[j]=dist[t]+v;
if(!v)q.push_front(j);
else q.push_back(j);
}
}
}
return dist[n]<=k;
}
signed main()
{
quick_cin();
cin>>n>>m>>k;
mset(h,-1,h);
while(m--)
{
int a,b,c;
cin>>a>>b>>c;
add(a,b,c),add(b,a,c);
}
int l=0,r=1e6+1;
while(l<r)
{
int mid=l+r>>1;
if(check(mid))r=mid;
else l=mid+1;
}
if(r==1e6+1)cout<<-1<<endl;
else cout<<r<<endl;
return 0;
}