hdu 3790 最短路径问题

最短路径问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4537    Accepted Submission(s): 1356


Problem Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
 

 

Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
 

 

Output
输出 一行有两个数, 最短距离及其花费。
 
Sample Input
3 2 1 2 5 6 2 3 4 5 1 3 0 0
 

 

Sample Output
9 11
 

 

Source
 

 

Recommend
notonlysuccess

 AC代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#define INF 1000000000
using namespace std;
struct node
{
 int to;
 int w;
 int ww;
}tedge;
vector<node>edge[40000];
int vis[40000],cnt[40000];
int dis[40000],cost[40000];
int n,m;
void add( int s,int e,int t,int tt)
{
 tedge.to=e;
 tedge.w=t;
 tedge.ww=tt;
 edge[s].push_back(tedge);
}
void spfa( int s)
{
 queue<int>q;
 int i;
 memset(vis,0,sizeof(vis));
 memset(cnt,0,sizeof(cnt));
 for( i=1;i<=n;i++)
  dis[i]=INF,cost[i]=INF;
 q.push(s);
 dis[s]=0,cost[s]=0;
 vis[s]=1;
 while(!q.empty( ))
 {
   int u=q.front( );
   q.pop( );
   vis[u]=0;
   for( i=0;i<edge[u].size( );i++)
   {
  int x=edge[u][i].to,y=edge[u][i].w,z=edge[u][i].ww;
  if(dis[x]-y>dis[u]||(dis[x]-y==dis[u]&&cost[x]-z>cost[u]))//此步关键
  {
     cost[x]=z+cost[u];
   dis[x]=y+dis[u];
      if(!vis[x])
  {
    q.push(x);
    vis[x]=1;
    if(++cnt[x]>n) return ;
     }
  }
 
   }
 }
 return ;
}
int main( )
{
  int i;
  int a,b,d,p;
  int s,e;
  while(scanf("%d%d",&n,&m),(n||m))
  {
 for( i=1;i<=n;i++)
  edge[i].clear( );
    while(m--)
    {
   scanf("%d%d%d%d",&a,&b,&d,&p);
   add(a,b,d,p);
   add(b,a,d,p);
    }
   
    scanf("%d%d",&s,&e);
    spfa(s);
    printf("%d %d\n",dis[e],cost[e]);
  }
  return 0;
}

链接:http://acm.hdu.edu.cn/showproblem.php?pid=3790

posted @ 2012-07-27 21:56  jiai  Views(134)  Comments(0Edit  收藏  举报