您的位置:首页 > 其它

最短路径问题3

2015-09-15 15:55 225 查看
http://acm.hdu.edu.cn/showproblem.php?pid=3790

用迪杰斯特拉算法编写的,感觉3个差不多。。。。。。。。


最短路径问题

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 18342 Accepted Submission(s): 5494



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

浙大计算机研究生复试上机考试-2010年

Recommend
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int Max=1<<30;
struct node{
int v,d,w;
node(int vv,int dd,int ww):v(vv),d(dd),w(ww){}
node(){}
};
bool operator <(const node &a,const node &b){
if(a.d>b.d)return true;
else if(a.d==b.d&&a.w>b.w)return true;
return false;
}
vector<vector<node> >G;
int N,M;
void dist(int s,int t){
priority_queue<node> q;
q.push(node(s,0,0));
bool used[1010];
memset(used,0,sizeof(used));
while(!q.empty()){
node p=q.top();
q.pop();
int u=p.v;
if(used[u])continue;
if(u==t){printf("%d %d\n",p.d,p.w);return;}
used[u]=true;
for(int i=0;i<G[u].size();i++){
int v=G[u][i].v;
int d=G[u][i].d;
int w=G[u][i].w;
if(used[v])continue;
q.push(node(v,d+p.d,w+p.w));
}
}
}
int main()
{
while(scanf("%d%d",&N,&M)&&N+M){
G.clear();
G.resize(N+10);
for(int i=0;i<M;i++){
int u,v,d,p;
scanf("%d%d%d%d",&u,&v,&d,&p);
G[u].push_back(node(v,d,p));
G[v].push_back(node(u,d,p));
}
int s,t;
scanf("%d%d",&s,&t);
dist(s,t);
}
return 0;
}
下午的收获就整理了下3个的代码。。。。哭了。。。。上课去了,咳。人生总有一些事实徒劳无功的。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: