zoukankan      html  css  js  c++  java
  • HDU 2544 最短路 (Dijkstra)

    在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt。但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗?

    Input
    输入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B<=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
    输入保证至少存在1条商店到赛场的路线。
    Output
    对于每组输入,输出一行,表示工作人员从商店走到赛场的最短时间
    Sample Input
    2 1
    1 2 3
    3 3
    1 2 5
    2 3 5
    3 1 2
    0 0
    Sample Output
    3
    2

    题意:

    题解:

    裸题,Dijkstra直接跑一遍就好了。

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=105,INF=0x3f3f3f3f;
    int cost[maxn][maxn];
    int d[maxn];
    bool used[maxn];
    int n;
    void dijkstra(int s)
    {
    	fill(d,d+n+1,INF);
    	fill(used,used+n+1,false);
    	d[s]=0;
    	while(true)
    	{
    		int v=-1;
    		for(int u=1;u<=n;u++)
    			if(!used[u]&&(v==-1||d[u]<d[v]))
    				v=u;
    		if(v==-1)
    			break;
    		used[v]=true;
    		for(int u=1;u<=n;u++)
    			d[u]=min(d[u],d[v]+cost[v][u]);
    	}
    }
    int main()
    {
    	ios::sync_with_stdio(false);
    	int m;
    	while(cin>>n>>m,n||m)
    	{
    		for(int i=1;i<=n;i++)
    			for(int j=i+1;j<=n;j++)
    				cost[i][j]=cost[j][i]=INF;
    		for(int i=0;i<m;i++)
    		{
    			int u,v,c;
    			cin>>u>>v>>c;
    			cost[u][v]=cost[v][u]=c;
    		}
    		dijkstra(1);
    		cout<<d[n]<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    Linux_KVM虚拟机
    Linux_KVM虚拟机
    Python_编程特色
    Python_编程特色
    Linux_进程管理&计划任务
    Linux_进程管理&计划任务
    Linux_系统破坏性修复实验
    Linux_系统破坏性修复实验
    Linux_Shell基础
    Confluence 6 的高级 Crowd 设置
  • 原文地址:https://www.cnblogs.com/orion7/p/7730083.html
Copyright © 2011-2022 走看看