zoukankan      html  css  js  c++  java
  • [10.2模拟] teach

    题意:给你一个有向图,你要尽可能地经过更多的城市并且希望总路程尽量小,每到一个城市,那个城市就会派出一名使者,使得走这个城市所属的环中的其他城市只要花费1的代价,求最多能经过多少城市以及最小路程

    题解:

    tarjan+spfa

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #define ll long long
    #define N 100010
    #define M 500010
    using namespace std;
    
    int n,m,e_num,dep,top,cnt,e_num2,ans1,ans2,tot;
    int nxt[M],to[M],w[M],h[N],nxt2[M],to2[M],w2[M],h2[N];
    int dfn[N],low[N],stk[N],num[N],bl[N],dp[N],val[N];
    bool in[N];
    
    queue<int> q;
    
    int gi() {
      int x=0,o=1; char ch=getchar();
      while(ch!='-' && (ch<'0' || ch>'9')) ch=getchar();
      if(ch=='-') o=-1,ch=getchar();
      while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
      return o*x;
    }
    
    void add(int x, int y, int z) {
      nxt[++e_num]=h[x],to[e_num]=y,w[e_num]=z,h[x]=e_num;
    } 
    
    void add2(int x, int y, int z) {
      nxt2[++e_num2]=h2[x],to2[e_num2]=y,w2[e_num2]=z,h2[x]=e_num2;
    }
    
    void tarjan(int u) {
      dfn[u]=low[u]=++dep;
      stk[++top]=u;
      for(int i=h[u]; i; i=nxt[i]) {
        int v=to[i];
        if(!dfn[v]) {
          tarjan(v);
          low[u]=min(low[u],low[v]);
        }
        else if(!bl[v]) low[u]=min(low[u],dfn[v]);
      }
      if(dfn[u]==low[u]) {
        int v;
        cnt++;
        while(1) {
          v=stk[top--],bl[v]=cnt,num[cnt]++;
          if(u==v) break;
        }
      }
    }
    
    void spfa() {
      memset(val,63,sizeof(val));
      dp[bl[1]]=num[bl[1]],val[bl[1]]=num[bl[1]]-1,in[bl[1]]=1,q.push(bl[1]);
      while(!q.empty()) {
        int u=q.front();
        in[u]=0,q.pop();
        for(int i=h2[u]; i; i=nxt2[i]) {
          int v=to2[i];
          if(dp[u]+num[v]>dp[v]) {
    	dp[v]=dp[u]+num[v];
    	val[v]=val[u]+w2[i]+num[v]-1;
    	if(!in[v]) q.push(v);
          }
          if(dp[u]+num[v]==dp[v] && val[v]>val[u]+w2[i]+num[v]-1)
    	val[v]=val[u]+w2[i]+num[v]-1;
        }
      }
    }
    
    int main() {
      n=gi(),m=gi();
      for(int i=1; i<=m; i++) {
        int x=gi(),y=gi(),z=gi();
        add(x,y,z);
      }
      for(int i=1; i<=n; i++) {
        if(!dfn[i]) tarjan(i);
      }
      for(int u=1; u<=n; u++) {
        for(int i=h[u]; i; i=nxt[i]) {
          int v=to[i];
          if(bl[u]!=bl[v]) add2(bl[u],bl[v],w[i]);
        }
      }
      spfa();
      for(int i=1; i<=cnt; i++) 
        if(dp[i]>ans1)
          ans1=dp[i],ans2=val[i];
        else if(dp[i]==ans1) ans2=min(ans2,val[i]);
      printf("%d %d", ans1,ans2);
      return 0;
    }
    
    
  • 相关阅读:
    【转】一个lucene的官网例子
    mongodb(回滚)
    mongodb( 实现join)
    JSON.stringify && JSON.parse
    js下的面向对象
    node(规则引擎)
    objective-c(内存管理)
    STM32F0xx_USART收发配置详细过程
    STM32F0xx_GPIO配置详细过程
    STM32F0_新建软件工程详细过程
  • 原文地址:https://www.cnblogs.com/HLXZZ/p/7625193.html
Copyright © 2011-2022 走看看