zoukankan      html  css  js  c++  java
  • 无向图找最小环

    无向图找最小环。

    #include <bits/stdc++.h>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    #define mp make_pair
    #define rep(i,l,r) for(int i=(l);i<=(r);++i)
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int N = 4e3+50;;
    const int M = 255;
    const int mod = 998244353;
    const int mo=123;
    const double pi= acos(-1.0);
    typedef pair<int,int>pii;
    typedef pair<ll,int>P;
    int n,m,tot;
    ll ans;
    vector<pii>edg[N];
    map<pii,int>p;
    ll dis[N];
    int vis[N],mark[N][N];
    int u[N],v[N],cost[N];
    ll dij(int s,int t,int add){
        priority_queue<P,vector<P>,greater<P> >q;
        for(int i=1;i<=tot;i++)vis[i]=0,dis[i]=10000000000;
        q.push(P(0LL,s));
        dis[s]=0;
        while(!q.empty()){
            int u=q.top().second;
            ll d=q.top().first;
            q.pop();
            vis[u]=1;
            if(d+add>=ans)break;
            for(auto e : edg[u]){
                int v=e.first;
                int w=e.second;
                if(mark[u][v])continue;
                if(dis[v]>dis[u]+w){
                    dis[v]=dis[u]+w;
                    if(!vis[v]){
                        q.push(P(dis[v],v));
                    }
                }
            }
        }
        return dis[t];
    }
    int main(){
        int T,cas=0;
        scanf("%d",&T);
        while(T--){
            scanf("%d",&m);
            tot=0;
            ans=10000000000;
            p.clear();met(mark,0);
            for(int i=1;i<N;i++)edg[i].clear();
            for(int i=0;i<m;i++){
                int x,y,xx,yy,w;
                scanf("%d%d%d%d%d",&x,&y,&xx,&yy,&w);
                if(!p.count(mp(x,y)))p[mp(x,y)]=++tot;
                if(!p.count(mp(xx,yy)))p[mp(xx,yy)]=++tot;
                edg[p[mp(x,y)]].pb(mp(p[mp(xx,yy)],w));
                edg[p[mp(xx,yy)]].pb(mp(p[mp(x,y)],w));
                u[i]=p[mp(x,y)];v[i]=p[mp(xx,yy)];cost[i]=w;
            }
            for(int i=0;i<m;i++){
                mark[u[i]][v[i]]=1;
                ans=min(ans,dij(u[i],v[i],cost[i])+cost[i]);
                mark[u[i]][v[i]]=0;
            }
            printf("Case #%d: %lld
    ",++cas,ans==10000000000?0:ans);
        }
        return 0;
    }
  • 相关阅读:
    查看JAVA占用CPU高的线程日志
    行为面试法(STAR)
    下载mysql document
    win10 子系统ubuntu中文乱码
    java 排序
    进制
    开始转型学习java
    java Collections工具类
    java Map实例
    java Map
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/7692591.html
Copyright © 2011-2022 走看看