题目:
链接:点击打开链接
题意:
思路:
dijjkstra算法。单源最短路径,更新路径的同时要更新花费。
代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 10000000 const int N = 1010; int n,m,a,b,d,c; int s,t; int map[N][N],cost[N][N]; int dis[N],val[N]; void dijkstra() { int vis[N]; memset(vis,0,sizeof(vis)); for(int i=1; i<=n; i++) { dis[i] = map[s][i]; val[i] = cost[s][i]; } for(int i=1; i<n; i++) { int x,minn = INF; for(int y=1; y<=n; y++) { if(!vis[y] && dis[y] < minn) { minn = dis[x=y]; } } vis[x] = 1; for(int y=1; y<=n; y++) { if(!vis[y] && map[x][y] < INF) { if(dis[y] > dis[x] + map[x][y]) { dis[y] = dis[x] + map[x][y]; val[y] = val[x] + cost[x][y]; } else if(dis[y] == dis[x] + map[x][y] && (val[y] > val[x] + cost[x][y])) { val[y] = val[x] + cost[x][y]; } } } } } int main() { //freopen("input.txt","r",stdin); while(scanf("%d%d",&n,&m) != EOF && (n || m)) { for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { map[i][j] = INF; cost[i][j] = INF; } } for(int i=1; i<=m; i++) { scanf("%d%d%d%d",&a,&b,&c,&d); if(map[a][b] > c) { map[a][b] = map[b][a] = c; cost[a][b] = cost[b][a] = d; } else if(map[a][b] == c && cost[a][b] > d) { cost[a][b] = cost[b][a] = d; } } scanf("%d%d",&s,&t); dijkstra(); printf("%d %d\n",dis[t],val[t]); } return 0; }---------------------------------------------------------------------------------
战斗,从不退缩;奋斗,永不停歇~~~~~~~~~~~~~~~~~