• 07-图4 哈利·波特的考试(最短路径)


    题目

    • 现在哈利·波特的手里有一本教材,里面列出了所有的变形魔咒和能变的动物。老师允许他自己带一只动物去考场,要考察他把这只动物变成任意一只指定动物的本事。于是他来问你:带什么动物去可以让最难变的那种动物(即该动物变为哈利·波特自己带去的动物所需要的魔咒最长)需要的魔咒最短?例如:如果只有猫、鼠、鱼,则显然哈利·波特应该带鼠去,因为鼠变成另外两种动物都只需要念4个字符;而如果带猫去,则至少需要念6个字符才能把猫变成鱼;同理,带鱼去也不是最好的选择。

    输入格式:

    • 输入说明:输入第1行给出两个正整数NN (le 100≤100)和MM,其中NN是考试涉及的动物总数,MM是用于直接变形的魔咒条数。为简单起见,我们将动物按1~NN编号。随后MM行,每行给出了3个正整数,分别是两种动物的编号、以及它们之间变形需要的魔咒的长度(le 100≤100),数字之间用空格分隔。

    输出格式:

    • 输出哈利·波特应该带去考场的动物的编号、以及最长的变形魔咒的长度,中间以空格分隔。如果只带1只动物是不可能完成所有变形要求的,则输出0。如果有若干只动物都可以备选,则输出编号最小的那只。

    输入样例:

    6 11
    3 4 70
    1 2 1
    5 4 50
    2 6 50
    5 6 60
    1 3 70
    4 6 60
    3 6 80
    5 1 100
    2 4 60
    5 2 80
    

    输出样例:

    4 70
    

    思路

    • 目的:让最难变的那种动物需要的魔咒最短。

    • 用邻接矩阵,用Floyd算法,记录各个顶点间最短距离。

    • 用WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex i,int N); 找到该顶点到其他顶点最难的魔咒

    • 再在这些最难的魔咒里选一个最短的。即让最难变的那种动物需要的魔咒最短。输出该Animal

    AC代码

    /*!
     * file 07-图4 哈利·波特的考试(最短路径).cpp
     *
     * author ranjiewen
     * date 2017/04/16 15:59
     *
     * 
     */
    
    #include <iostream>
    #include <cstdio>
    #include <cstdlib> 
    using namespace std;
    
    #define MaxVertexNum 100    /* 最大顶点数设为100 */
    #define INFINITY 65535        /* 设为双字节无符号正数的最大值65535*/
    typedef int Vertex;         /* 用顶点下标表示顶点,为整型 */
    typedef int WeightType;        /* 边的权值设为整型 */
    
    /* 边的定义 */
    typedef struct ENode *PtrToENode;
    struct ENode{
    	Vertex V1, V2;      /* 有向边<V1, V2> */
    	WeightType Weight;  /* 权重 */
    };
    typedef PtrToENode Edge;
    
    /* 图结点的定义 */
    typedef struct GNode *PtrToGNode;
    struct GNode{
    	int Nv;  /* 顶点数 */
    	int Ne;  /* 边数   */
    	WeightType G[MaxVertexNum][MaxVertexNum]; /* 邻接矩阵 */
    };
    typedef PtrToGNode MGraph; /* 以邻接矩阵存储的图类型 */
    
    MGraph CreateGraph(int VertexNum);
    void InsertEdge(MGraph Graph, Edge E);
    MGraph BuildGraph();
    void Floyd(MGraph Graph, WeightType D[][MaxVertexNum]);
    WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex i, int N);
    void FindAnimal(MGraph Graph);
    
    int main()
    {
    	MGraph graph;
    	graph = BuildGraph();
    	FindAnimal(graph);
    	return 0;
    }
    
    MGraph CreateGraph(int VertexNum)
    { /* 初始化一个有VertexNum个顶点但没有边的图 */
    	Vertex V, W;
    	MGraph Graph;
    
    	Graph = (MGraph)malloc(sizeof(struct GNode)); /* 建立图 */
    	Graph->Nv = VertexNum;
    	Graph->Ne = 0;
    	/* 初始化邻接矩阵 */
    	/* 注意:这里默认顶点编号从0开始,到(Graph->Nv - 1) */
    	for (V = 0; V < Graph->Nv; V++)
    	for (W = 0; W < Graph->Nv; W++)
    		Graph->G[V][W] = INFINITY;
    
    	return Graph;
    }
    
    void InsertEdge(MGraph Graph, Edge E)
    {
    	/* 插入边 <V1, V2> */
    	Graph->G[E->V1][E->V2] = E->Weight;
    	/* 若是无向图,还要插入边<V2, V1> */
    	Graph->G[E->V2][E->V1] = E->Weight;
    }
    
    MGraph BuildGraph()
    {
    	MGraph Graph;
    	Edge E;
    	int Nv, i;
    
    	scanf("%d", &Nv);   /* 读入顶点个数 */
    	Graph = CreateGraph(Nv); /* 初始化有Nv个顶点但没有边的图 */
    
    	scanf("%d", &(Graph->Ne));   /* 读入边数 */
    	if (Graph->Ne != 0) { /* 如果有边 */
    		E = (Edge)malloc(sizeof(struct ENode)); /* 建立边结点 */
    		/* 读入边,格式为"起点 终点 权重",插入邻接矩阵 */
    		for (i = 0; i < Graph->Ne; i++) {
    			scanf("%d %d %d", &E->V1, &E->V2, &E->Weight);
    			E->V1--;    //编号从0开始 
    			E->V2--;
    			/* 注意:如果权重不是整型,Weight的读入格式要改 */
    			InsertEdge(Graph, E);
    		}
    	}
    
    	return Graph;
    }
    
    /* 邻接矩阵存储 - 多源最短路算法 */
    
    void Floyd(MGraph Graph, WeightType D[][MaxVertexNum])
    {
    	Vertex i, j, k;
    
    	/* 初始化 */
    	for (i = 0; i < Graph->Nv; i++)
    	for (j = 0; j < Graph->Nv; j++) {
    		D[i][j] = Graph->G[i][j];
    	}
    
    	for (k = 0; k < Graph->Nv; k++)
    	for (i = 0; i < Graph->Nv; i++)
    	for (j = 0; j < Graph->Nv; j++)
    	if (D[i][k] + D[k][j] < D[i][j]) {
    		D[i][j] = D[i][k] + D[k][j];
    	}
    }
    
    void FindAnimal(MGraph Graph)
    {
    	WeightType D[MaxVertexNum][MaxVertexNum], MaxDist, MinDist;
    	Vertex Animal;
    
    	Floyd(Graph, D);
    
    	MinDist = INFINITY;
    	for (Vertex i = 0; i < Graph->Nv; i++) {
    		MaxDist = FindMaxDist(D, i, Graph->Nv);
    		if (MaxDist == INFINITY) {    //说明有从i无法变出的动物 //表示图有不连通的,该动物不能变成任何其他动物
    			printf("0
    ");
    			return;
    		}
    		if (MinDist > MaxDist) {    //找到最长距离更小的动物 
    			MinDist = MaxDist;    //更新距离 
    			Animal = i + 1;        //记录编号 
    		}
    	}
    	printf("%d %d
    ", Animal, MinDist);
    }
    
    WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex i, int N)
    {
    	WeightType MaxDist;
    	MaxDist = 0;
    	for (Vertex j = 0; j < N; j++)//找出i到其他动物j的最长距离 
    	if (i != j && D[i][j] > MaxDist) //排除对角线上的元素
    		MaxDist = D[i][j];
    	return MaxDist;
    }
    
    

    Reference

  • 相关阅读:
    SpringBoot 之Spring Boot Starter依赖包及作用
    WebApi过滤器使用和请求实体合法性验证
    sql server 事务隔离性 snapshot 、read committed说明
    jQuery的deferred对象详解
    Juqery插件编写 基础说明
    sqlServer MERGE 对数据进行修改或插入自动视别 特别用于批量数据的插入或修改
    .net EntityFramework dbContext 如何实例化
    NOPI Excel 数据导入到数据库
    知识点整理01- 引用对象被子方法赋值后不改变;CheckBox 取消选择不可用问题
    .net Windows Service 按装及遇到的问题
  • 原文地址:https://www.cnblogs.com/ranjiewen/p/6719005.html
Copyright © 2020-2023  润新知