• UOJ#494K点最短路


    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #define N 100007
    #define Q 100000000007
    using namespace std;
    int n, m, k, s, t, spe[15], tot, ls[N], cnt;
    long long ans, dis[N], d[15][15];
    bool b[N], v[40];
    queue<int> q;
    
    struct edge{
        int to, next, w;
    }e[N];
    
    inline int read(){
    	int s=0,w=1;
    	char ch=getchar();
    	while(ch<'0'||ch>'9'){
    		if(ch=='-'){
    			w=-1;
    		}
    		ch=getchar();
    	}
    	while(ch>='0'&&ch<='9'){
    		s=s*10+ch-'0';
    		ch=getchar();
    	}
    	return s*w;
    }
    
    void add(int x,int y,int z){
        e[++tot].to=y;
        e[tot].w=z;
        e[tot].next=ls[x];
        ls[x]=tot;
    }
    
    void spfa(int x){
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++){
            dis[i]=Q;
        }
        dis[x]=0;
        q.push(x);
        while (!q.empty()){
            int now=q.front();
            q.pop();
            for(int i=ls[now];i;i=e[i].next){
                if(dis[now]+e[i].w<dis[e[i].to]){
                    dis[e[i].to]=dis[now]+e[i].w;
                    if(!b[e[i].to]){
                        q.push(e[i].to);
                        b[e[i].to]=1;
                    }
                }
            }
            b[now]=0;
        }
        cnt++;
        for(int i=1;i<=k+1;i++){
            if(cnt!=i){
    			d[cnt][i]=dis[spe[i]];
    		}
    	}
        d[cnt][k+2]=dis[t];
    }
    
    void dfs(int dep, long long sum, int dc){
        if(sum+d[dc][k+2]>ans){
    		return;
    		} 
        if(dep>=k+1){
            if(sum+d[dc][k+2]<ans){
    			ans=sum+d[dc][k+2];
    		}
            return;
        }
        for(int i=2;i<=k+1;i++){
            if(!v[i]){
                v[i]=1;
                dfs(dep+1,sum+d[dc][i],i);
                v[i]=0;
            }
        }
    }
    
    int main(){
    	n=read();
    	m=read();
    	k=read();
    	s=read();
    	t=read();
        for(int i=1;i<=m;i++){
            int x, y, z;
            x=read();
            y=read();
            z=read();
            add(x,y,z);
        }
        for(int i=2;i<=k+1;i++){
        	spe[i]=read();
        }
        spe[1]=s;
        for(int i=1;i<=k+1;i++){
            spfa(spe[i]);
        }
        ans=Q;
        dfs(1,0,s);
        if (ans<Q){
    		printf("%lld",ans);
    	}
        else{
    		printf("-1");
    	}
    }
    

      

  • 相关阅读:
    为 Jenkins 配置 .NET 持续集成环境
    树莓派连接GPS模块
    杂记
    大型网站架构系列:电商网站架构案例
    我的心博客。
    555555555555111
    powershell 环境变量只有当前会话起作用问题
    powershell 下独立silent 安装 浏览器问题
    python编码格式
    scss教程推荐
  • 原文地址:https://www.cnblogs.com/hahaha2124652975/p/11351216.html
Copyright © 2020-2023  润新知