2017 9 11 noip模拟赛T2
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=205; int map[N][N]; int d[N],tag[N],book[N],f[N]; int n,m; void work(int x) { memset(d,63,sizeof(d)); memset(book,0,sizeof(book)); memset(f,0,sizeof(f)); d[x]=0; for(int i=1;i<=n;i++) { int minx=2*1e9,t=0; for(int j=1;j<=n;j++) if(!book[j]&&d[j]<minx) minx=d[j],t=j; book[t]=1; for(int j=1;j<=n;j++) if(!book[j]) { if(d[j]>d[t]+map[t][j]) d[j]=d[t]+map[t][j],f[j]=t; else if(d[j]==d[t]+map[t][j]) f[j]=0; } } // printf("\n"); for(int i=1;i<=n;i++) if(f[i]&&f[i]!=x) tag[f[i]]=1; // for(int i=1;i<=n;i++) printf("%d ",f[i]); // printf("\n"); } int main() { freopen("city.in","r",stdin); freopen("city.out","w",stdout); memset(map,63,sizeof(map)); scanf("%d %d",&n,&m); int a,b,c; for(int i=1;i<=m;i++) { scanf("%d %d %d",&a,&b,&c); map[a][b]=map[b][a]=min(map[a][b],c); } for(int i=1;i<=n;i++) work(i); int flag=0; for(int i=1;i<=n;i++) { if(tag[i]) flag=1,printf("%d ",i); } if(flag==0) printf("No important cities.\n"); return 0; }
look look :