源代码

/* Programming: ebola virus vaccines from the world's three major manufacturers to the  centers for disease 
                control and prevention(station) in order to several serious disaster area in west Africa
*/   
#include <windows.h>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#define N 1000    //km
#define inf 999999   //Set an infinite variable
using namespace std;

int map[N][N];       // Record figure path length between two points
int dis[N];          // According to the current point to the source point of the shortest path length
int vis[N];          //visited-Whether to have access to at some point array tag
int s,e;             //Declared a global variable starting point to the end

void Dijkstra(int n) //Dijkstra algorithm
{
    int i,j;
    int pos;
    for(i=1; i<=n; i++)
    {
        dis[i]=map[1][i];//For the first time to dis array assignment
    }
    vis[1]=1;            //Tag starting point has been visited
    for(i=1; i<=n-1; i++)//Run again n - 1, the rest of the n - 1 point
    {
        pos=1;
        int min=inf;
        for(j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]<min)
            {
                min=dis[j];//Update the minimum distance
                pos=j;     //And tag the subscript
            }
        }
        vis[pos]=1;        //Mark has been visited GaiDian
        for(j=1; j<=n; j++)
        {
            if(!vis[j]&&dis[j]>dis[pos]+map[pos][j])//Update directly adjacent vertices with j dis values
                dis[j]=dis[pos]+map[pos][j];        //Here is the key to the algorithm updates
        }
    }
    if(dis[n]!=inf)
        printf("%d\n",dis[n]);                    //Print out the source to the last point of the shortest path length;
    else
        printf("There is no the shortest path\n");//Otherwise print("There is no the shortest path");
}

//The main function
int main()
{
    int n,m;                        //n表示点的个数,m表示边的个数
    int i,j;
    int a,b;
	double x;
	//Declare a variable
	cout<<"         -----欢迎进入埃博拉病毒疫运输优化系统------       "<<endl;
	cout<<"加载中··稍等"<<endl;
	Sleep(1500);
	system("cls");
	cout<<"系统采用最短路径的优化方案,将全球埃博拉病毒的疫苗生产商和西非灾区典型的城市构建网络图"<<endl;
	cout<<"将生产商地区和西非地区抽象成节点,它们之间的关系用抽象的连线表示"<<endl;
	cout<<"请输入你所输入两个数据:n:节点的个数 m: 连线的个数"<<endl;
    while(scanf("%d %d",&n,&m)!=EOF)//Loop input data values of n and m
    {
        memset(vis,0,sizeof(vis));  //Empty the vis array
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                map[i][j]=inf;       //Ownership value initialized to the largest, on behalf of the points on the map without the attachment
            }
            map[i][i]=0;             //Distance itself to its own weight to 0
        }
        for(i=1; i<=m; i++)
        {
			cout<<"将节点分别依次编号,现在输入三个数据,a: 表示第一个城市 ;b: 表示第二个城市,w:表示两城市之间的距离"<<endl;
            scanf("%d %d %llf",&a,&b,&x);
            if(map[a][b]>x)           //update
                map[a][b]=map[b][a]=x;//So says the undirected graph
        }
		cout<<"请输入疫苗生产商:起点;请输入灾区地点:终点"<<endl;
        //scanf("%d %d",&s,&e);         //Enter start and end
        Dijkstra(n);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值