标签:题解 输入 复试 ++ logs int blog chm cep
题目链接:【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): 25620 Accepted Submission(s): 7650
题解:最短路问题容易解决:最多有1000个顶点,用邻接矩阵保存边,并维护重边,DIJ(O(n^2))可以解决。在维护最短路的同时维护最小费用。如果dis[u]>dis[v]+E[u][v]||(dis[u]==dis[v]+mp[u][v]&&CO[u]>CO[v]+mp[u][v])。
#include<bits/stdc++.h> using namespace std; const int INF = 1e7 + 15; const int maxn = 2050; int N, M; int E[maxn][maxn], CO[maxn][maxn]; int dis[maxn], cost[maxn], vis[maxn]; void DIJ(int st, int ed) { for(int i = 1; i <= N; i++) dis[i] = cost[i] = INF; dis[st] = cost[st] = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= N; i++) { int x = -1, len = INF + 15; for(int j = 1; j <= N; j++) if(!vis[j] && len > dis[j]) len = dis[x = j]; if(x == -1) break; vis[x] = 1; for(int v = 1; v <= N; v++) { if(dis[v] > dis[x] + E[x][v] || (dis[v] == dis[x] + E[x][v] && cost[v] > cost[x] + CO[x][v])) { dis[v] = dis[x] + E[x][v]; cost[v] = cost[x] + CO[x][v]; } } } } int main () { while(~scanf("%d%d", &N, &M)) { if(N == 0 && M == 0) break; int u, v, len, co; for(int i = 1; i <= N; i++) { for(int j = 1; j <= N; j++) E[i][j] = CO[i][j] = INF; E[i][i] = CO[i][i] = 0; } for(int i = 1; i <= M; i++) { scanf("%d%d%d%d", &u, &v, &len, &co); if(E[u][v] > len || (E[u][v] == len && CO[u][v] > co)) { E[u][v] = E[v][u] = len; CO[u][v] = CO[v][u] = co; } } int st, ed; scanf("%d%d", &st, &ed); DIJ(st, ed); printf("%d %d\n", dis[ed], cost[ed]); } return 0; }
标签:题解 输入 复试 ++ logs int blog chm cep
原文地址:http://www.cnblogs.com/pealicx/p/6677443.html