L3-022 地铁一日游
floyd算法建立新图,dfs标记~
#include<bits/stdc++.h> using namespace std; const int maxn=1010; const int inf=1e9; int d[maxn][maxn]; vector<int> g[maxn]; bool visit[maxn]; bool isend[maxn]; int N,M,K; void floyd () { for (int k=1;k<=N;k++) for (int i=1;i<=N;i++) for (int j=1;j<=N;j++) if (i!=j) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } void dfs (int s) { visit[s]=true; for (int i=0;i<g[s].size();i++) { if (visit[g[s][i]]==false) { dfs (g[s][i]); } } } int main () { scanf ("%d %d %d",&N,&M,&K); int u,v,distance; for (int i=1;i<=N;i++) for (int j=1;j<=N;j++) d[i][j]=inf; for (int i=0;i<M;i++) { scanf ("%d",&u); isend[u]=true; while (1) { scanf ("%d %d",&distance,&v); d[u][v]=min(d[u][v],distance); d[v][u]=d[u][v]; u=v; char ch=getchar (); if (ch=='\n') break; } isend[u]=true; } floyd (); for (int i=1;i<=N;i++) { unordered_map<int,int> pos; for (int j=1;j<=N;j++) { if (i!=j&&d[i][j]>pos[d[i][j]/K+2]&&d[i][j]!=inf) pos[d[i][j]/K+2]=d[i][j]; } for (int j=1;j<=N;j++) if (i!=j) { if (d[i][j]==pos[d[i][j]/K+2]||(isend[j]==true&&d[i][j]!=inf)) g[i].push_back(j); } } int q; scanf ("%d",&q); int s; for (int i=0;i<q;i++) { scanf ("%d",&s); fill (visit,visit+maxn,false); dfs (s); int flag=0; for (int j=1;j<=N;j++) { if (visit[j]==true) { if (flag!=0) printf (" "); printf ("%d",j); flag++; } } printf ("\n"); } return 0; }