标签:style http os io for 问题 html ar
题目链接:最短路径问题
两个权值的最短路问题
SFPA +前向星 水过250ms
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <queue> const int INF = 1e7; using namespace std; int n,m,t; int ma[1001][1001],dis[1001],cost[1001]; bool vis[1001]; struct node{ int u,v,w, ww; int next; }edge[100001]; int head[100001]; void init() { memset(head,0,sizeof(head)); for(int i = 1;i<=n;i++) { dis[i] = INF; cost[i] = INF; vis[i] = 0; } t = 1; } int s,e; void add(int a,int b,int c,int d) { edge[t].v = b; edge[t].w = c; edge[t].ww = d; edge[t].next = head[a]; head[a] = t++; } void SPFA() { queue<int>q; q.push(s); vis[s] = true; dis[s] = 0; cost[s] = 0; // int sum = 0; while(!q.empty()) { int p = q.front(); // printf("%d",p); q.pop(); vis[p] = 0; if(head[p]) { for(int pp = head[p];pp!=0;pp = edge[pp].next) { // printf("->%d\n",edge[pp].v); if(dis[p] + edge[pp].w < dis[edge[pp].v]) { dis[edge[pp].v] = dis[p] + edge[pp].w ; cost[edge[pp].v] = cost[p] + edge[pp].ww; // sum += edge[pp].ww; if(!vis[edge[pp].v]) { q.push(edge[pp].v); vis[edge[pp].v] = true; } } else if(dis[p] + edge[pp].w == dis[edge[pp].v]) { if(cost[edge[pp].v] > cost[p] + edge[pp].ww) { cost[edge[pp].v] = cost[p] + edge[pp].ww; } } } } } printf("%d %d\n",dis[e],cost[e]); } int main() { int a,b,c,d; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0 && m==0) break ; init(); for(int i = 0;i<m;i++) { scanf("%d%d%d%d",&a,&b,&c,&d); add(a,b,c,d); add(b,a,c,d); } scanf("%d%d",&s,&e); SPFA(); } return 0; }
HDU 3790 -最短路径问题,布布扣,bubuko.com
标签:style http os io for 问题 html ar
原文地址:http://blog.csdn.net/wjw0130/article/details/38235545