![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<algorithm> using namespace std; const int maxn = 315; const int maxm = 5015; const int inf = 9999999; int cnt,head[ maxn ]; int n,m,k; int dis[ maxn ],vis[ maxn ]; struct node{ int u,val,next; }edge[ maxm ]; struct node2{ int city_number,dis; double speed,time; int p_number;//p_number is for the number of the people }p[ maxn ]; void init(){ cnt=0; memset( head,-1,sizeof( head )); } void addedge( int a,int b,int c ){ edge[ cnt ].u=b; edge[ cnt ].val=c; edge[ cnt ].next=head[ a ]; head[ a ]=cnt++; } int cmp( node2 a,node2 b ){ if( a.time!=b.time ) return a.time<b.time; else if( a.dis!=b.dis ) return a.dis>b.dis; else //return a.city_number>b.city_number; return a.p_number>b.p_number; } void spfa( int s ){ for( int i=1;i<=n;i++ ){ dis[ i ]=inf; vis[ i ]=0; } queue<int>q; while( !q.empty() ) q.pop(); dis[ s ]=0; vis[ s ]=1; q.push( s ); while( !q.empty() ){ int now=q.front(); q.pop(); vis[ now ]=0; for( int i=head[ now ];i!=-1;i=edge[ i ].next ){ int next=edge[ i ].u; if( dis[ next ]>dis[ now ]+edge[ i ].val ){ dis[ next ]=dis[ now ]+edge[ i ].val; if( vis[ next ]==0 ){ vis[ next ]=1; q.push( next ); } } } } return ; } int main(){ while( scanf("%d%d%d",&n,&m,&k)!=EOF ){ int i; int a,b,c; init(); while( k-- ){ scanf("%d%d%d",&a,&b,&c); addedge( b,a,c );//change the dir } int zz_pos; scanf("%d",&zz_pos); for(i=1;i<=m;i++ ){ scanf("%d",&p[i].city_number); } for(i=1;i<=m;i++ ){ scanf("%lf",&p[i].speed); } spfa( zz_pos );//the aim to all the vet,get dis[ maxn ] for(i=1;i<=m;i++ ){ p[i].p_number=i; if( dis[ p[i].city_number ]>=inf ){ p[i].time=1.0*inf; p[i].dis=inf; } else{ p[i].dis=dis[ p[i].city_number ]; p[i].time=dis[ p[i].city_number ]*1.0/(1.0*p[i].speed); } } sort( p+1,p+1+m,cmp ); if( p[1].time>=(1.0*inf) ){ printf("No one\n"); } else{ printf("%d\n",p[1].p_number); } } return 0; }
WA了很多次,发现最后不用排序就过不了,还有排序必须用sort
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<string.h> #include"algorithm" using namespace std; #define M 999999999 int a[310],b[310],map[310][310]; struct node { int id,begin,speed,dist; double time; }f[310]; int cmp(node a,node b) { if(a.time!=b.time ) return a.time<b.time; else if(a.dist!=b.dist) return a.dist>b.dist; else return a.id>b.id; } int MIN(int x,int y) { if(x>y) return y; else return x; } int main() { int n,m,i,j,p,l,x,y,z,min; while(scanf("%d%d%d",&n,&m,&l)!=-1) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(i==j) map[i][j]=0; else map[i][j]=M; } } for(i=1;i<=l;i++) { scanf("%d%d%d",&x,&y,&z); if(map[y][x]>z) map[y][x]=z; } int start; scanf("%d",&start); memset(f,0,sizeof(f)); memset(b,0,sizeof(b)); for(i=1;i<=n;i++) a[i]=map[start][i]; int t,num=1; a[start]=0; b[start]=1; t=j=start; while(num<n) { min=M; for(i=1;i<=n;i++) { if(b[i]==0&&map[t][i]) { a[i]=MIN(a[i],a[t]+map[t][i]); if(min>a[i]) { min=a[i]; j=i; } } } b[j]=1; t=j; num++; } for(i=1;i<=m;i++) { scanf("%d",&p); f[i].begin=p; f[i].id=i; } int q; for(i=1;i<=m;i++) { scanf("%d",&q); f[i].speed=q; f[i].dist=a[f[i].begin]; f[i].time=1.0*f[i].dist/f[i].speed; } for(i=1;i<=m;i++) { if(f[i].dist!=M) break; } if(i==m+1) printf("No one\n"); else { sort(f+1,f+m+1,cmp); printf("%d\n",f[1].id); } } return 0; }