标签:include tle 输入 inf 最短路径问题 string color des iostream
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33061 Accepted Submission(s):
9713
#include<queue> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 100010 #define INF 0x3f3f3f3f using namespace std; queue<int>que; int n,m,s,t,tot; int a[1005][1005],b[1005][1005]; int dis[1005],pay[1005],vis[1005]; int to[MAXN*2],net[MAXN*2],cap[MAXN*2],sum[MAXN*2],head[1005]; void add(int u,int v,int w,int ww){ to[++tot]=v;cap[tot]=w;sum[tot]=ww;net[tot]=head[u];head[u]=tot; to[++tot]=u;cap[tot]=w;sum[tot]=ww;net[tot]=head[v];head[v]=tot; } void spfa(){ while(!que.empty()) que.pop(); memset(vis,0,sizeof(vis)); memset(dis,0x3f,sizeof(dis)); memset(pay,0x3f,sizeof(pay)); que.push(s);vis[s]=1; dis[s]=0;pay[s]=0; while(!que.empty()){ int now=que.front(); que.pop();vis[now]=0; for(int i=head[now];i;i=net[i]) if(dis[to[i]]>dis[now]+cap[i]){ dis[to[i]]=dis[now]+cap[i]; pay[to[i]]=pay[now]+sum[i]; if(!vis[to[i]]){ vis[to[i]]=1; que.push(to[i]); } } else if(dis[to[i]]==dis[now]+cap[i]&&pay[now]+sum[i]<pay[to[i]]){ pay[to[i]]=pay[now]+sum[i]; if(!vis[to[i]]){ vis[to[i]]=1; que.push(to[i]); } } } } int main(){ while(scanf("%d%d",&n,&m)&&n!=0&&m!=0){ for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i==j) a[i][j]=b[i][j]=0; else a[i][j]=b[i][j]=INF; for(int i=1;i<=m;i++){ int x,y,z,zz; scanf("%d%d%d%d",&x,&y,&z,&zz); if(a[x][y]>z){ a[x][y]=a[y][x]=z; b[x][y]=b[y][x]=zz; } } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) if(a[i][j]!=INF) add(i,j,a[i][j],b[i][j]); scanf("%d%d",&s,&t); spfa(); printf("%d %d\n",dis[t],pay[t]); tot=0;memset(head,0,sizeof(head)); } return 0; }
标签:include tle 输入 inf 最短路径问题 string color des iostream
原文地址:https://www.cnblogs.com/cangT-Tlan/p/8968348.html