zoukankan      html  css  js  c++  java
  • hdu2544最短路

    我的第一个最短路的题目。

    坑的是,题目说路径C最大是1000.我就把最大值INF设成了10000;

    结果WA了,好几次,找不出错。。最后改了INF=0x3f3f3f3f才A的。。。哎。。数据怎么搞得,切!!!

    #include <iostream>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const int MAXV=105;
    int cost[MAXV][MAXV];
    void Dijkstra(int n)
    {
    	int dist[MAXV];
    	bool s[MAXV];
    	for(int i=1;i<=n;i++)
    	{
    		dist[i]=cost[1][i];
    		s[i]=0;
    	}
    	s[1]=1;
    	int k;
    	for(int i=1;i<=n;i++)
    	{
    		int min=INF;
    		for(int j=1;j<=n;j++)
    		{
    			if(s[j]==0&&dist[j]<min)
    			{
    				k=j;
    				min=dist[j];
    			}
    		}
    		s[k]=1;
    		for(int j=1;j<=n;j++)
    		{
    			if(s[j]==0)
    				if(cost[k][j]<INF&&dist[k]+cost[k][j]<dist[j])
    					dist[j]=dist[k]+cost[k][j];
    		}
    	}
    	printf("%d
    ",dist[n]);
    }
    int main()
    {
    
    	int n,m;
    	while(scanf("%d%d",&n,&m))
    	{
    		if(n==0&&m==0)
    			break;
    		for(int i=1;i<=n;i++)
    		{
    			for(int j=1;j<=n;j++)
    			{
    					cost[i][j]=INF;
    			}
    		}
    		int a,b,c;
    		while(m--)
    		{
    			scanf("%d%d%d",&a,&b,&c);
    			cost[a][b]=cost[b][a]=c;
    		}
    		Dijkstra(n);
    	}
    	return 0;
    }


  • 相关阅读:
    git指令-撤销修改
    git指令-管理修改
    jquery高级
    jquery
    sql的练习题
    git指令-工作区和暂存区
    java-多线程安全-锁
    oracle习题-emp表查询练习
    java-异常进阶-包的使用
    oracle-函数总结
  • 原文地址:https://www.cnblogs.com/unclejelly/p/4082139.html
Copyright © 2011-2022 走看看