标签:style http color os io for ar cti
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define inf 99999999 using namespace std; struct E { int v,w,next; } edge[5000]; int head[1000],cnt,n,m,k,dis[1000][1000],vis[1000]; int _hash[1000]; void spfa(int s) { queue<int>Q; int i,j; for(i=1; i<=m; i++) { dis[s][i]=inf; vis[i]=0; } dis[s][s]=0; vis[s]=1; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(i=head[u]; i!=-1; i=edge[i].next) { if(dis[s][edge[i].v]>dis[s][u]+edge[i].w) { dis[s][edge[i].v]=dis[s][u]+edge[i].w; if(!vis[edge[i].v]) { vis[edge[i].v]=1; Q.push(edge[i].v); } } } } } void add(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } int main() { int i,j,t,u,v,w; while(~scanf("%d%d%d",&n,&m,&k)) { cnt=0; memset(head,-1,sizeof(head)); memset(_hash,0,sizeof(_hash)); memset(edge,0,sizeof(edge)); for(i=1; i<=n; i++) { scanf("%d",&t); _hash[t]++; } for(i=1; i<=k; i++) { scanf("%d%d%d",&u,&v,&w); add(u,v,w); add(v,u,w); } for(i=1; i<=m; i++) { spfa(i); } int minn=inf,sum; for(i=1; i<=m; i++) { sum=0; for(j=1; j<=m; j++) { if(_hash[j]) sum+=dis[i][j]*_hash[j]; } if(minn>sum) { minn=sum; } } printf("%d\n",minn); } return 0; }
#include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define inf 99999999 using namespace std; struct E { int v,w,next; } edge[50000]; int head[1000],dis[1000],vis[1000],cnt,m,_hash[1000]; void add(int u,int v,int w) { edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } void spfa(int s) { int i,j; for(i='A'; i<='z'; i++) { dis[i]=inf; vis[i]=0; } vis[s]=1; dis[s]=0; queue<int>Q; Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop(); vis[u]=0; for(i=head[u]; i!=-1; i=edge[i].next) { if(dis[edge[i].v]>dis[u]+edge[i].w) { dis[edge[i].v]=dis[u]+edge[i].w; if(!vis[edge[i].v]) { vis[edge[i].v]=1; Q.push(edge[i].v); } } } } } int main() { int i,j,w; char a[10],b[10]; while(~scanf("%d",&m)) { memset(head,-1,sizeof(head)); memset(_hash,0,sizeof(_hash)); memset(edge,0,sizeof(edge)); cnt=0; for(i=1; i<=m; i++) { scanf("%s%s%d",a,b,&w); int u=a[0]; int v=b[0]; _hash[u]=1; _hash[v]=1; add(u,v,w); add(v,u,w); } spfa('Z'); int minn=inf,u; for(i='A'; i<'Z'; i++) { if(_hash[i]) { if(minn>dis[i]) { minn=dis[i]; u=i; } } } printf("%c %d\n",u,minn); } }
标签:style http color os io for ar cti
原文地址:http://blog.csdn.net/juncoder/article/details/38393499