POJ2607/ZOJ1857 (最短路...注意题意及输入控制)

题目链接:  http://poj.org/problem?id=2607

               http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=857

 

其实啥都不想说,万恶的题意,万恶到输入都不给说明...

 

minimize the maximum distance from any intersection to the nearest fire station.

好吧, 这一句把我绕得~看了别人的才知道是这么个状况,加入的fire station不是为整个city服务

而是为了几个别complaint的村庄而计划的,,,

 

上代码,注释都赖得写了:

 zoj

2473375 2011-04-04 14:20:24 Accepted 1857 C++ 10 500 zzuli_ㄉㄉ

   poj

12 8429467 dooder_daodao 232K 47MS C++ 2229B 2011-04-04 14:06:07

 

#include<cstdio> #include<cstring> #include<queue> using namespace std; #define M 508 #define N 20008 #define INF 0x7fffffff typedef struct talQNode{ int v,c; bool operator < (const talQNode &a) const { return a.c<c; } }QNode; typedef struct{ int u,v,c; int next; }Edge; Edge edge[N]; int head[M]; int dis[M],disl[M]; bool vis[M],inq[M]; int fire[M]; int f,n,e; priority_queue<QNode>q; void AddEdge(int u,int v,int c) { edge[e].u=u;edge[e].v=v;edge[e].c=c; edge[e].next=head[u];head[u]=e++; edge[e].v=u;edge[e].u=v;edge[e].c=c; edge[e].next=head[v];head[v]=e++; } void Dijkstra(int f) { QNode cur,next; int u,v,tmp; memset(vis,false,n+1); cur.v=f;cur.c=0; q.push(cur); while(!q.empty()){ cur=q.top();q.pop(); u=cur.v; if(vis[u]) continue; dis[u]=cur.c; vis[u]=true; for(tmp=head[u];tmp!=-1;tmp=edge[tmp].next){ v=edge[tmp].v; if(vis[v]) continue; if(dis[v]-dis[u]>edge[tmp].c){ dis[v]=dis[u]+edge[tmp].c; next.v=v;next.c=dis[v]; q.push(next); } } } } int SPFA(int f) { QNode cur,next; int rel,u,v,tmp; memset(inq,false,n+1); cur.v=f;cur.c=0; inq[f]=true;disl[f]=0; q.push(cur); while(!q.empty()){ cur=q.top();q.pop(); u=cur.v; inq[u]=false; for(tmp=head[u];tmp!=-1;tmp=edge[tmp].next){ v=edge[tmp].v; if(disl[v]-disl[u]>edge[tmp].c){ disl[v]=disl[u]+edge[tmp].c; if(!inq[v]){ inq[v]=true; next.v=v; next.c=disl[v]; q.push(next); } } } } rel=0; for(int i=1;i<=n;i++){ if(disl[i]>rel) rel=disl[i]; } return rel; } void InitDis() { int i; for(i=0;i<=n;i++) dis[i]=INF; for(i=0;i<f;i++) Dijkstra(fire[i]); } int Find() { int ret=0,i,min=INF,tmp; for(i=1;i<=n;i++){ memmove(disl,dis,4*(n+1)); tmp=SPFA(i); if(tmp<min){ ret=i; min=tmp; } } return ret; } int main() { int i,u,v,c; char s[108]; while(~scanf("%d%d",&f,&n)){ memset(head,-1,sizeof(head)); e=0; for(i=0;i<f;i++) scanf("%d",fire+i); getchar(); while(gets(s)&&s[0]){ sscanf(s,"%d%d%d",&u,&v,&c); AddEdge(u,v,c); } InitDis(); printf("%d/n",Find()); } return 0; }

你可能感兴趣的:(POJ2607/ZOJ1857 (最短路...注意题意及输入控制))