BZOJ 5180 [Baltic2016]Cities(斯坦纳树)
作者:互联网
include
include
include
include
include
include
using namespace std;
define int long long
const int INF=1e16;
const int N=101000;
const int M=201000;
struct edge{
int to,nxt,w;
}e[M*2];
int cnt,head[N];
void add_edge(int u,int v,int w){
cnt++;
e[cnt].nxt=head[u];
e[cnt].to=v;
e[cnt].w=w;
head[u]=cnt;
}
struct node{
int id,w;
node(int idd=0,int ww=0){
id=idd,w=ww;
}
};
bool operator <(node a,node b){
return a.w>b.w;
}
priority_queue
bool vis[N];
void dij(int dis){
memset(vis,0,sizeof(vis));
while(!q.empty()){
node x=q.top();
q.pop();
int u=x.id;
if(vis[u])continue;vis[u]=1;
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(dis[v]>dis[u]+e[i].w){
dis[v]=dis[u]+e[i].w;
q.push(node(v,dis[v]));
}
}
}
}
int read(){
int sum=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){sum=sum10+ch-'0';ch=getchar();}
return sum*f;
}
int n,k,m,dp[50][N],a[10];
signed main(){
n=read();k=read();m=read();
for(int i=0;i<(1<<k);i++)
for(int j=1;j<=n;j++)
dp[i][j]=INF;
for(int i=1;i<=k;i++){
int x=read();dp[1<<(i-1)][x]=0;
}
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
add_edge(u,v,w);add_edge(v,u,w);
}
for(int i=1;i<(1<<k);i++){
for(int j=i;j;j=(j-1)&i)
for(int k=1;k<=n;k++)
dp[i][k]=min(dp[i][k],dp[j][k]+dp[i^j][k]);
for(int k=1;k<=n;k++)if(dp[i][k]!=INF)q.push(node(k,dp[i][k]));
dij(dp[i]);
}
int ans=INF;
for(int i=1;i<=n;i++)ans=min(ans,dp[(1<<k)-1][i]);
printf("%lld",ans);
return 0;
}
标签:5180,ch,int,cnt,include,斯坦纳,read,Cities,dp 来源: https://www.cnblogs.com/Xu-daxia/p/10436838.html