B0->途经堡垒1->...->B
总耗费能量 武器总价值
输入样例:
6 12
1 2 10 5
2 3 16 20
3 1 4 2
2 4 20 22
4 5 2 2
5 3 12 6
4 6 8 5
6 5 10 5
6 1 20 25
1 5 8 5
2 5 2 1
2 6 8 5
4
2 3 6 5
输出样例:
5
5->2
2 1
5->1->3
12 7
5->4->6
10 7
5
0 0
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pii pair<int,int>
const int N=55;
int a[N];
int n,k,m;
//map<int,int> mp;
int mp[255*4+5];
signed main(){//用数组模拟哈希表可以节省一半的时间
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
for(int l=k+1;l<n;l++){
mp[a[i]+a[j]+a[k]+a[l]]=1;
}
}
}
}
int x;
// cout<<mp[44]<<endl;
while(k--){
cin>>m;
int fg=1;
for(int i=0;i<m;i++){
cin>>x;
// if(!mp.count(x*4)){
if(!mp[x*4]){
fg=0;
// cout<<"No";
break;
}
}
if(!fg)cout<<"NO";
else cout<<"YES";
// if(fg)cout<<"Yes";
cout<<endl;
}
return 0;
}
//Yes or YES
//break跳出本层循环没错,别阻挡了输入
#include<bits/stdc++.h>
using namespace std;
// #define int long long
const int inf=0x3f3f3f3f;
const int N=1005;
int e[N][N];
struct edge{
int to,w,w2;
};
vector<edge> G[N];
int n,m,u,v,w,w2;
struct node{
int to,w,w2;
bool operator<(const node & p)const{
if(w==p.w)return w2<p.w2;
else return w>p.w;
}
};
int dist[N],dist1[N];
bool vis[N];
int path[N];
void print(int x){
if(path[x]==-1)return ;
print(path[x]);
cout<<"->"<<x;
}
signed main(){
memset(e,0x3f,sizeof(e));
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>u>>v>>w>>w2;
// e[u][v]=min(e[u][v],w);
// e[v][u]=min(e[v][u],w);
e[u][v]=e[v][u]=w;
G[u].push_back({v,w,w2});
G[v].push_back({u,w,w2});
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(e[i][j]>e[i][k]+e[k][j])e[i][j]=e[i][k]+e[k][j];
}
}
}
int rt=-1;
int mx=inf;
for(int i=1;i<=n;i++){
int tmp=-1;
for(int j=1;j<=n;j++){
tmp=max(tmp,e[i][j]);
}
if(tmp<mx){
rt=i;
mx=tmp;
}
}
priority_queue<node> Q;
Q.push({rt,0,0});
for(int i=1;i<=n;i++){
dist[i]=inf;
path[i]=-1;
}
dist[rt]=0;
for(int t=1;t<=n;t++){
if(Q.empty())break;
node p=Q.top();Q.pop();
int u=p.to;
if(vis[u]){
t--;
continue;
}
vis[u]=true;
for(int i=0;i<G[u].size();i++){
int to=G[u][i].to;
int w=G[u][i].w;
int w2=G[u][i].w2;
if(dist[to]>dist[u]+w){
dist[to]=dist[u]+w;
dist1[to]=dist1[u]+w2;
path[to]=u;
Q.push({to,dist[to],dist1[to]});
}
else if(dist[to]==dist[u]+w&&dist1[to]<dist1[u]+w2){
// dist[to]=dist[u]+w;
dist1[to]=dist1[u]+w2;
path[to]=u;
Q.push({to,dist[to],dist1[to]});
}
}
}
// cout<<rt<<"hhhhhhh"<<endl;
cout<<rt<<endl;
int T,x;
cin>>T;
while(T--){
cin>>x;
cout<<rt;
print(x);
cout<<endl;
cout<<dist[x]<<" "<<dist1[x]<<endl;
}
return 0;
}