zoukankan      html  css  js  c++  java
  • 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");
    	}
    }
    

      

  • 相关阅读:
    hadoop无法启动常见原因
    mahout版本兼容问题
    递归打印字符串
    斐波那契数列
    int('x', base)中的base参数
    打印的特殊符号
    位置参数,默认参数,非关键字可变长参数,关键字可变长参数
    try...except包含try...finally方法
    7.9 未解答
    对集合应用符号 | & ^
  • 原文地址:https://www.cnblogs.com/hahaha2124652975/p/11351216.html
Copyright © 2011-2022 走看看