题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3790
最短路问题,因为结果会超int所以最终结果要用__int64位保存。当距离相等的时候,去费用小的路走。
#include<iostream> #include<queue> using namespace std; #define INF (1<<30) struct node { int v,d,cost; node *next; }*head[1005],edge[100005*2],*p; bool vis[1005]; __int64 dis[1005],cost[1005]; int n,m; void insert(int start,int end,int d,int cost) { p->v = end; p->d = d; p->cost = cost; p->next = head[start]; head[start] = p++; } void spfa(int start) { int i; for(i = 1; i <= n; i++) dis[i] = cost[i] = INF; memset(vis,false,sizeof(vis)); vis[start] = true; dis[start] = 0; cost[start] = 0; queue<int>Q; Q.push(start); while(!Q.empty()) { int now = Q.front(); Q.pop(); vis[now] = false; for(node *p = head[now]; p ; p = p->next) { if(dis[p->v] > dis[now] + p->d) { dis[p->v] = dis[now] + p->d; cost[p->v] = cost[now]+p->cost; if(!vis[p->v]) { vis[p->v] = true; Q.push(p->v); } } else if(dis[p->v] == dis[now] + p->d && cost[p->v] > cost[now] + p->cost) { cost[p->v] = cost[now] + p->cost; if( !vis[p->v]) { vis[p->v] = true; Q.push(p->v); } } } } } int main() { int s,t,i,a,b,d,q; while(scanf("%d%d",&n,&m) != EOF && (n || m)) { for(i = 1; i <= n; i++) head[i] = NULL; p = edge; while(m --) { scanf("%d%d%d%d",&a,&b,&d,&q); insert(a,b,d,q); insert(b,a,d,q); } scanf("%d%d",&s,&t); spfa(s); printf("%I64d %I64d\n",dis[t],cost[t]); } return 0; }