Countries in War 强连通

  强连通分量之间权值为0

强连通加最短路

//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
//input by bxd
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);--i)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m)
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define pb push_back
#define REP(i,N)  for(int i=0;i<(N);i++)
#define CLR(A,v)  memset(A,v,sizeof A)
//////////////////////////////////
#define inf 0x3f3f3f3f
const int N=500+5;
const int M=N*N;
int head[M],pos;
struct Edge
{
    int nex,to,v;
}edge[M];
struct node
{
    int s,e,v;
}s[M];
void add(int a,int b,int c)
{
    edge[++pos].nex=head[a];
    head[a]=pos;
    edge[pos].v=c;
    edge[pos].to=b;
}
int tot,ind,cnt,Stack[N],dfn[N],low[N],vis[N],belong[N];
int dis[N];
int mp[N][N];
int n,m;
void init()
{
    tot=ind=cnt=pos=0;
    CLR(Stack,0);
    CLR(head,0);
    CLR(vis,0);
    CLR(dfn,0);
    CLR(low,0);
    rep(i,1,n)
    rep(j,1,n)
    if(i!=j)mp[i][j]=inf;
    else mp[i][j]=0;

}
void tarjan(int x)
{
    dfn[x]=low[x]=++tot;
    Stack[++ind]=x;
    vis[x]=1;
    for(int i=head[x];i;i=edge[i].nex)
    {
        int v=edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[x]=min(low[x],low[v]);
        }
        else if(vis[v])
            low[x]=min(low[x],low[v]);
    }
    if(dfn[x]==low[x])
    {
        cnt++;int v;
        do
        {
            v=Stack[ind--];
            vis[v]=0;
            belong[v]=cnt;
        }
        while(x!=v);
    }
}


int main()
{
    while(RII(n,m),n||m)
    {
        init();
        rep(i,1,m)
        {
            int a,b,c;RIII(a,b,c);

            add(a,b,c);
        }
        rep(i,1,n)
        if(!dfn[i])tarjan(i);


        rep(i,1,n)
        {
            int u=belong[i];
            for(int j=head[i];j;j=edge[j].nex)
            {
                int v=belong[ edge[j].to ];
                if(u!=v)
                {
                    mp[u][v]=min(mp[u][v],edge[j].v);
                }
            }
        }

        int k;
        RI(k);
        int last=-1;
        while(k--)
        {
            int a,b;
            RII(a,b);
            a=belong[a];
            b=belong[b];
            if(a==last)
            {
                if(dis[b]==inf)
                    printf("Nao e possivel entregar a carta
");
                else
                    printf("%d
",dis[b]);
            }
            else
            {
                last=a;
                rep(i,1,cnt)
                dis[i]=inf;
                dis[a]=0;
                CLR(vis,0);
                while(1)
                {
                    int u=-1,minn=inf;
                    rep(i,1,cnt)
                    if(!vis[i]&&dis[i]<minn)
                        minn=dis[u=i];
                    if(u==-1)break;
                    vis[u]=1;
                    rep(i,1,cnt)
                    dis[i]=min(dis[i],dis[u]+mp[u][i]);
                }
                if(dis[b]==inf)
                    printf("Nao e possivel entregar a carta
");
                else
                    printf("%d
",dis[b]);
            }
        }
        cout<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/bxd123/p/10799790.html