标签:pid color memory logs memset 坑爹 jks out min
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3790
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25271 Accepted Submission(s): 7541
#include "cstdio" #include "cstring" #include "algorithm" #define inf 0x3f3f3f3f int dis[1002],vis[1002],cost[1002];///dis存储各点到点s的长度 typedef struct{///路径模型 int lenth;///路长 int cost;///路费 }Path; Path map1[1002][1002];///地图 void dijkstra(int n,int s,int t) { memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++){ dis[i]=map1[s][i].lenth; cost[i]=map1[s][i].cost; } int pos=1; dis[s]=0; vis[s]=1; for(int k=1;k<n;k++){ int min1=inf; for(int i=1;i<=n;i++){ if(!vis[i]&&min1>dis[i]){ min1=dis[i]; pos=i; } } vis[pos]=1; for(int i=1;i<=n;i++){ int l=dis[pos]+map1[pos][i].lenth; if(!vis[i]&&dis[i]>=l){ if(dis[i]==l){///找到相等路径时,选取花费少的 cost[i]=std::min(cost[i],cost[pos]+map1[pos][i].cost); } else///无条件选择路径短的 { cost[i]=cost[pos]+map1[pos][i].cost; dis[i]=l; } } } } } int main() { int n,m,i,j; Path p; while(~scanf("%d%d",&n,&m)&&n&&m){ for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { map1[i][j].lenth=inf; } } int a,b,c,cost1; for(j=0;j<m;j++) { scanf("%d%d%d%d",&a,&b,&c,&cost1); ///可能会出现重边!!! if(c<map1[a][b].lenth){ map1[a][b].lenth=map1[b][a].lenth=c;///保留距离较短的 map1[a][b].cost=map1[b][a].cost=cost1; } else if(map1[a][b].lenth==c&&map1[a][b].cost>cost1)///保留费用较少的 { map1[a][b].cost=map1[b][a].cost=cost1; } } int s,t; scanf("%d%d",&s,&t); dijkstra(n,s,t); printf("%d %d\n",dis[t],cost[t]); } return 0; }
标签:pid color memory logs memset 坑爹 jks out min
原文地址:http://www.cnblogs.com/kimsimple/p/6561150.html