其他分享
首页 > 其他分享> > P1841 [JSOI2007]重要的城市(最短路+拓扑)

P1841 [JSOI2007]重要的城市(最短路+拓扑)

作者:互联网

找无向图中的某些点,使得删去这些点后会使得\(A\)到\(B\)的最短路变长,其中\(A\)和\(B\)都不等于给定的点

考虑对每个点跑一次dij,然后可以重建出一张最短路的新图,然后对新图做一次拓扑排序,如果发现某个点的出边终点的入度为1,那么就标记该点即可

注意一个特判:

标记点不能等于当前最短路的起点,因为新图相当于一张DAG,而DAG是不能回头的,况且题目要求\(A\)和\(B\)都不等于给定的点

代码:

#include <bits/stdc++.h>
#define int long long
#define sci(a) scanf("%lld",&a)
#define scii(a,b) scanf("%lld%lld",&a,&b)
#define sciii(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define scc(a) scanf("%c",&a)
#define scs(a) scanf("%s",a)
#define pri(a) printf("%lld",a)
#define prc(a) printf("%c",a)
#define prs() printf(" ")
#define prn() printf("\n")
#define re(i,a,b) for(int i=a;i<=b;++i)
#define fo(i,a,b) for(int i=a;i<b;++i)
#define rre(i,a,b) for(register i=a;i>=b;--i)
#define all(i,a) for(auto i=a.begin();i!=a.end();++i)
#define mkp make_pair
#define fst first
#define snd second
#define frt front
#define bak back
#define pub(a) push_back(a)
#define pob() pop_back
#define puf(a) push_front(a)
#define pof() pop_front
#define mem0(a) memset(a,0,sizeof(a))
#define memmx(a) memset(a,0x3f3f,sizeof(a))
#define memmn(a) memset(a,-0x3f3f,sizeof(a))
#define nmsl cout<<"NMSL"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define FAST ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);
using namespace std;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vei;
typedef vector<pii> vep;
typedef vector<char> vec;
typedef vector<string> ves;
typedef map<int,int> mpii;
typedef map<pii,int> mppi;
typedef map<char,int> mpci;
typedef map<string,int> mpsi;
typedef deque<int> dqi;
typedef deque<pii> dqp;
typedef deque<char> dqc;
typedef deque<string> dqs;
typedef priority_queue<int> mxpi;
typedef priority_queue<int,vei,greater<int>> mnpi;
typedef priority_queue<pii> mxpp;
typedef priority_queue<pii,vep,greater<pii>> mnpp;
const int maxn=500005;
const int inf=0x3f3f3f3f3f3f3f3f;
const db eps=1e-10;
const db pi=3.1415926535;
int lowb(int x){return x&-x;}
int mmax(int a,int b,int c){return max(max(a,b),c);}
int mmin(int a,int b,int c){return min(min(a,b),c);}
int ll(int p){return p<<1;}
int rr(int p){return p<<1|1;}
int mm(int l,int r){return (l+r)/2;}
int lg(int x){if(x==0) return 1;return (int)log2(x)+1;}
bool smleql(db a,db b){if(a<b||fabs(a-b)<=eps)return true;return false;}
bool bigeql(db a,db b){if(a>b||fabs(a-b)<=eps)return true;return false;}
bool eql(db a,db b){if(fabs(a-b)<eps) return 1;return 0;}
db len(db a,db b,db c,db d){return sqrt((a-c)*(a-c)+(b-d)*(b-d));}
int gcd(int x,int y){return __gcd(x,y);}
int lcm(int x,int y){return x*y/__gcd(x,y);}
bool isp(int x){if(x==1)return false;if(x==2)return true;for(int i=2;i*i<=x;++i)if(x%i==0)return false;return true;}
int qpow(int a,int b,int mod){int tmp=a%mod,ans=1;while(b){if(b&1) ans=(ans*tmp)%mod;tmp=(tmp*tmp)%mod;b>>=1;}return ans;}
inline int read(){char ch=getchar();int s=0,w=1;while(ch<48||ch>57){if(ch=='-')w=-1;ch=getchar();}while(ch>=48&&ch<=57){s=(s<<1)+(s<<3)+ch-48;ch=getchar();}return s*w;}
inline void write(int x){if(x<0)putchar('-'),x=-x;if(x>9)write(x/10);putchar(x%10+48);}

int n,m;
vei e[205],val[205],ee[205];
int x[maxn],y[maxn],z[maxn],dis[205],deg[205],dd[205];
bool is[205],v[205];

void gao2(int s){
    memmx(dis);
    mem0(v);
    dis[s]=0;
    mnpp q;
    q.push(mkp(0,s));
    while(!q.empty()){
        int t=q.top().snd;
        q.pop();
        if(v[t]) continue;
        fo(i,0,e[t].size()){
            int to=e[t][i];
            if(dis[to]>dis[t]+val[t][i]){
                dis[to]=dis[t]+val[t][i];
                q.push(mkp(dis[to],to));
            }
        }
    }
    mem0(deg);mem0(dd);
    re(i,1,n) ee[i].clear();
    re(i,1,m){
        if(dis[x[i]]+z[i]==dis[y[i]]){
            ee[x[i]].pub(y[i]);
            deg[y[i]]++;
        }
        if(dis[y[i]]+z[i]==dis[x[i]]){
            ee[y[i]].pub(x[i]);
            deg[x[i]]++;
        }
    }
    queue<int> qq;
    re(i,1,n) if(deg[i]==0) qq.push(i);
    re(i,1,n) dd[i]=deg[i];
    while(!qq.empty()){
        int f=qq.front();qq.pop();
        for(auto y:ee[f]){
            if(dd[y]==1&&f!=s)
                is[f]=1;
            deg[y]--;
            if(deg[y]==0) qq.push(y);
        }
    }
}

void gao(){
    re(i,1,n) gao2(i);
    int cnt=0;
    re(i,1,n) if(is[i]) cout<<i<<' ',cnt++;
    if(!cnt) puts("No important cities."); 
}

signed main(){
    FAST
    cin>>n>>m;
    re(i,1,m){
        cin>>x[i]>>y[i]>>z[i];
        e[x[i]].pub(y[i]);
        e[y[i]].pub(x[i]);
        val[x[i]].pub(z[i]);
        val[y[i]].pub(z[i]);
    }
    gao();
    return 0;
}

标签:typedef,JSOI2007,int,短路,dis,re,deg,P1841,define
来源: https://www.cnblogs.com/oneman233/p/11820806.html