标签:n+1 pac ems init ret 无法 line int 数据
(内部题目不放题面了) 题面
#include<bits/stdc++.h>
using namespace std;
#define rint register int
const int maxn = 2e4 + 10;
int head[maxn << 3];
int Head[maxn << 3];
int cnt;
int fz[] = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384};
int ans;
int dis[maxn];
int vis[maxn];
struct node{
int next,to,dis;
}a[maxn << 3];
void add(int x,int y,int z){
a[++cnt].next = head[x];
a[cnt].to = y;
a[cnt].dis = z;
head[x] = cnt;
}
void init(){
cnt = 0;
ans = INT_MAX;
memset(head,0,sizeof(head));
}
deque<int> q;
void spfa(int s){
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[s] = 0; vis[1] = 1;
q.push_front(s);
while(!q.empty()){
rint u = q.front(); q.pop_front();
vis[u] = 0;
for(rint i = head[u];i;i = a[i].next){
rint v = a[i].to;
if(dis[v] > dis[u] + a[i].dis){
dis[v] = dis[u] + a[i].dis;
if(!vis[v]) {
if(q.empty()) q.push_front(v);
else if(dis[v] < dis[q.front()]) q.push_front(v);
else q.push_back(v);
}
vis[v] = 1;
}
}
}
}
int main(){
freopen("leave.in","r",stdin);
freopen("leave.out","w",stdout);
int T;scanf("%d",&T);
while(T--){
init();
int n,m;scanf("%d%d",&n,&m);
for(rint i = 1;i <= m;++i){
int u,v,z;scanf("%d%d%d",&u,&v,&z);
add(u,v,z);add(v,u,z);
}
for(rint i = head[1];i;i = a[i].next) Head[a[i].to] = head[a[i].to];
rint now = cnt;
for(rint i = 0;n >> i;++i){
cnt = now;
for(rint j = head[1];j;j = a[j].next){
rint v = a[j].to;
if(v & (1 << i)) add(n + 1,v,a[j].dis);
else add(v,n + 2,a[j].dis);
}
spfa(n+1);
ans = min(ans,dis[n+2]);
for(rint j = head[1];j;j = a[j].next) head[a[j].to] = Head[a[j].to];
head[n + 1] = head[n + 2] = 0;
}
if(ans != 0x3f3f3f3f) cout << ans << endl;
else cout << "-1" << endl;
}
return 0;
}
标签:n+1 pac ems init ret 无法 line int 数据
原文地址:https://www.cnblogs.com/2004-08-20/p/13615893.html