P17176「MSOI R1」莫追 题解
这估计会是这题题解中最猎奇的一个了。
首先考虑暴力,使用 dfs 枚举删去那几个点,然后对剩下的点进行最短路求最小值,时间复杂度
接下来考虑一些优化,首先 dfs 遍历图,然后枚举走的点,如果点数
接下来是最重要的剪枝!!!!!
注意到如果从当前节点走最短路(不考虑删除的话)还比当前的最优解大,也可以直接结束别走了,这样子就能用神秘的做法水过这一道题了。如果从每一个点都跑一边到
赛时雷霆代码:
#include<bits/stdc++.h>
using namespace std;
namespace fast_IO{
#define FASTIO
#define IOSIZE 100000
char ibuf[IOSIZE],obuf[IOSIZE];char*p1=ibuf,*p2=ibuf,*p3=obuf;
#ifdef ONLINE_JUDGE
#define getchar()((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x)((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif
#define isdigit(ch)(ch>47&&ch<58)
#define isspace(ch)(ch<33)
template<typename T>inline T read(){T s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*w;}template<typename T>inline bool read(T&s){s=0;int w=1;char ch;while(ch=getchar(),!isdigit(ch)and(ch!=EOF))if(ch=='-')w=-1;if(ch==EOF)return false;while(isdigit(ch))s=s*10+ch-48,ch=getchar();return s*=w,true;}inline bool read(char&s){while(s=getchar(),isspace(s));return true;}inline bool read(char*s){char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))*s++=ch,ch=getchar();*s='\000';return true;}template<typename T>inline void print(T x){if(x<0)putchar('-'),x=-x;if(x>9)print(x/10);putchar(x%10+48);}inline void print(char x){putchar(x);}inline void print(char*x){while(*x)putchar(*x++);}inline void print(const char*x){for(int i=0;x[i];i++)putchar(x[i]);}
#ifdef _GLIBCXX_STRING
inline bool read(std::string&s){s="";char ch;while(ch=getchar(),isspace(ch));if(ch==EOF)return false;while(!isspace(ch))s+=ch,ch=getchar();return true;}inline void print(std::string x){for(int i=0,n=x.size();i<n;i++)putchar(x[i]);}
#endif
template<typename T,typename...T1>inline int read(T&a,T1&...other){return read(a)+read(other...);}template<typename T,typename...T1>inline void print(T a,T1...other){print(a);print(other...);}struct Fast_IO{~Fast_IO(){fwrite(obuf,p3-obuf,1,stdout);}}io;template<typename T>Fast_IO&operator>>(Fast_IO&io,T&b){return read(b),io;}template<typename T>Fast_IO&operator<<(Fast_IO&io,T b){return print(b),io;}
}using namespace fast_IO;
int n,m;
long long ans=1e18,dis[505];
bitset<505> vis;
vector<pair<int,int>> g[505],g2[505];
struct node{
int u,val;
bool operator < (const node &b) const{
return val>b.val;
}
};
priority_queue<node> q;
inline void dfs(int u,long long num,int cnt){
if(num>ans) return ;
if(cnt>n/2||(u!=n&&cnt==n/2)) return ;
if(u==n){
ans=num;
return ;
}
for(int i=0;i<g[u].size();++i){
int v=g[u][i].first,w=g[u][i].second;
if(num+dis[v]>ans) continue;
if(!vis[v]){
vis[v]=1;
dfs(v,num+w,cnt+1);
vis[v]=0;
}
}
}
bool cmp(pair<int,int> x,pair<int,int> y){
return x.second<y.second;
}
signed main(){
io>>n>>m;
for(int i=1;i<=m;++i){
int u,v,w;
io>>u>>v>>w;
g[u].push_back({v,w});
g2[v].push_back({u,w});
}
for(int i=1;i<=n;++i) sort(g[i].begin(),g[i].end(),cmp);
memset(dis,0x3f,sizeof dis);
q.push({n,0});
dis[n]=0;
while(!q.empty()){
int u=q.top().u;
q.pop();
for(int i=0;i<g2[u].size();++i){
int v=g2[u][i].first,w=g2[u][i].second;
if(dis[u]+w<dis[v]){
dis[v]=dis[u]+w;
q.push({v,dis[v]});
}
}
}
dfs(1,0,1);
io<<(ans==1e18?-1:ans);
return 0;
}