bfs样例求调
  • 板块UVA11367 Full Tank?
  • 楼主紊莫turtle
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/12/27 19:05
  • 上次更新2023/10/24 06:23:57
查看原帖
bfs样例求调
443675
紊莫turtle楼主2022/12/27 19:05

170我输出180。
思路没大错,写法很清真

//Author: Velvet on Luogu(uid=443675)
#include <bits/stdc++.h>
using namespace std;

inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
inline void writeln(int x){write(x);putchar('\n');}
inline void writesp(int x){write(x);putchar(' ');}
inline int lowbit(int x) {return x&(-x);}
int p[1005];
int d[1005][1005],vis[1005][1005];
struct sta{
	int pos,f,co;
	bool operator <(sta A)const{
		return co>A.co;
	}
};
int main(){
	int n=read(),m=read();
	for(int i=1;i<=n;i++) p[i]=read();
	memset(d,-1,sizeof d);
	for(int i=1;i<=m;i++){
		int x=read()+1,y=read()+1,z=read();
		d[x][y]=d[y][x]=z;
	}
	int T=read();
	while(T--){
		memset(vis,0,sizeof vis);
		int c=read(),s=read()+1,e=read()+1;
		priority_queue<sta> Q;
		Q.push({s,0,0});int ans=0x7f7f7f7f;
		while(Q.size()){
			int now=Q.top().pos,f=Q.top().f,co=Q.top().co;
			Q.pop();
			if(vis[now][f]) continue;
			vis[now][f]=1;
			if(now==e){ans=co;break;}
			if(f<c)
				Q.push({now,f+1,co+p[now]});
			for(int i=1;i<=n;i++)
				if(d[now][i]!=-1&&d[now][i]<f)
					Q.push({i,f-d[now][i],co});
		}
		if(ans==0x7f7f7f7f) printf("impossible\n");
		else writeln(ans);
	}
    return 0;
}

2022/12/27 19:05
加载中...