nefu 663 Emergency(flord算法)

Emergency

Time Limit 3000ms

Memory Limit 65536K

description

Kudo’s real name is not Kudo. Her name is Kudryavka Anatolyevna Strugatskia, and Kudo is only her nickname.
Now, she is facing an emergency in her hometown:
Her mother is developing a new kind of spacecraft. This plan costs enormous energy but finally failed. What’s more, because of the failed project, the government doesn’t have enough resource take measure to the rising sea levels caused by global warming, lead to an island flooded by the sea.
Dissatisfied with her mother’s spacecraft and the government, civil war has broken out. The foe wants to arrest the spacecraft project’s participants and the “Chief criminal” – Kudo’s mother – Doctor T’s family.
At the beginning of the war, all the cities are occupied by the foe. But as time goes by, the cities recaptured one by one.
To prevent from the foe’s arrest and boost morale, Kudo and some other people have to distract from a city to another. Although they can use some other means to transport, the most convenient way is using the inter-city roads. Assuming the city as a node and an inter-city road as an edge, you can treat the map as a weighted directed multigraph. An inter-city road is available if both its endpoint is recaptured.
Here comes the problem.
Given the traffic map, and the recaptured situation, can you tell Kudo what’s the shortest path from one city to another only passing the recaptured cities?

input

The input consists of several test cases.
The first line of input in each test case contains three integers N (0< N≤ 300), M (0< M≤ 100000) and Q (0< Q≤ 100000), which represents the number of cities, the numbers of inter-city roads and the number of operations.
Each of the next M lines contains three integer x, y and z, represents there is an inter-city road starts from x, end up with y and the length is z. You can assume that 0< z≤ 10000.
Each of the next Q lines contains the operations with the following format:
a)	0 x – means city x has just been recaptured.
b)	1 x y – means asking the shortest path from x to y only passing the recaptured cities.
The last case is followed by a line containing three zeros.

output

For each case, print the case number (1, 2 …) first.
For each operation 0, if city x is already recaptured (that is,the same 0 x operation appears again), print “City x is already recaptured.”
For each operation 1, if city x or y is not recaptured yet, print “City x or y is not available.” otherwise if Kudo can go from city x to city y only passing the recaptured cities, print the shortest path’s length; otherwise print “No such path.”
Your output format should imitate the sample output. Print a blank line after each test case.

sample_input

3 3 6
0 1 1
1 2 1
0 2 3
1 0 2
0 0
0 2
1 0 2
1 2 0
0 2

0 0 0

sample_output

Case 1:
City 0 or 2 is not available.
3
No such path.
City 2 is already recaptured.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#define INF 1000000000
using namespace std;
int root[310];
int dist[310][310];
int n;
void init()
{
    for(int i=0; i<n; i++)
        for(int j=0; j<n; j++)
        {
            if(i!=j) dist[i][j]=INF;
            else dist[i][j]=0;
        }
}
void solve(int k)
{
    int i,j;
    for(i=0;i<n;i++)
    //if(root[i])
    {
        for(j=0;j<n;j++)
         //if(root[j])
         dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
    }
}

int main()
{
     int m,p;
     int x,y,z;
     int t=1;
     while(scanf("%d%d%d",&n,&m,&p)!=EOF)
     {
         if(n==0&&m==0&&p==0) break;
         memset(root,0,sizeof(root));
         init();
         for(int i=0;i<m;i++)
         {
             scanf("%d%d%d",&x,&y,&z);
             if(dist[x][y]>z) dist[x][y]=z;
         }
         printf("Case %d:\n",t++);
         while(p--)
         {
             scanf("%d",&x);
             if(x==0)
             {
                 scanf("%d",&y);
                 if(!root[y])
                {
                    root[y]=1;
                    solve(y);
                }
                else printf("City %d is already recaptured.\n",y);
             }
             else
                {
                      scanf("%d%d",&y,&z);
                       if(root[y]==0||root[z]==0)
                    printf("City %d or %d is not available.\n",y,z);
                else
                {
                    if(dist[y][z]<INF)
                        printf("%d\n",dist[y][z]);
                    else printf("No such path.\n");
                }
             }
         }
         puts("");
     }

    return 0;
}

nefu 663 Emergency(flord算法)

时间: 2025-01-17 06:52:37

nefu 663 Emergency(flord算法)的相关文章

NEFU 1112 粉刷栅栏算法

题目链接 中文题 简单搜索题 例数据 输入 6 1 1 1 1 9 9 输出 3 注意是每一个递归搜索都返回一个min 而不是只有总的返回min #include <cstdio> #include <algorithm> #include <cstring> using namespace std; int a[5678]; int dfs(int l,int r,int k) { if(l>r||(l==r&&a[l]<=k)) retu

算法学习笔记三(插点法)

插点法,也就是Flord算法,主要应用于 求出有权图中最短路径. 算法核心: for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++) { if(d[i][k]+d[k][j]<d[i][j]) d[i][j]=d[i][k]+d[k][j]; }

图-最短路径

最短路径:对于网图来说,最短路径是指两个顶点之间经过的边上权值之和最少的路径,并且我们称路径上的第一个顶点式源点,最后一个顶点是终点.以下图为例,  寻找v0到v8的最短距离. 对应解决思路:现在比较成熟的有Dijkstra(迪杰斯特拉)算法和Flord算法算法. Dijkstra(迪杰斯特拉)算法 Dijkstra求源节点S到终结点D的最短距离的过程中,循环过程中每次确定一个源节点到其它节点T的最短距离.该距离确定后,遍历判断该节点T到其它节点的距离是否比现在S到其它节点距离短,更短的话则修改

Emergency(山东省第一届ACM程序设计真题+Floyd算法变型)

题目描述 Kudo's real name is not Kudo. Her name is Kudryavka Anatolyevna Strugatskia, and Kudo is only her nickname. Now, she is facing an emergency in her hometown: Her mother is developing a new kind of spacecraft. This plan costs enormous energy but f

1003 Emergency(考察迪杰斯特拉算法+第二标尺)

大致题意就是给出一个图.每个顶点的点权.顶点之间的边权.起点和终点.求出从起点到终点的最短路径的数量.以及最短路径上的最大点权之和. 这是一道模板题,要先记住大体流程,然后反复练习,较难头疼... 1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 510; 5 const int inf = 0x3fffffff;//无穷大 6 //第一标尺 7 int n,m,st,ed,G[maxn][maxn];//顶点个数

迪杰斯特拉算法——PAT 1003

本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究就会发现越经典. 首先可以将整个图的节点看成两个集合:一个是S,一个是U-S.如果是求v0到图中各点的最短距离的话,那么S就是已经确认到v0距离最短的点,U-S则是对于整体的点集合U,还没有加入S集合的点. 这里提出一个算法总体的思想,将所有的点按照一定的原则加入到S集就是解集.而这个解法就是重点了

模板化的七种排序算法,适用于T* vector&lt;T&gt;以及list&lt;T&gt;

最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板纯属于偷懒,更方便于测试代码的有效性,等代码写完也懒得去改了.下面开始介绍这段代码,有什么不对的地方欢迎前来指正. 一共写了七种排序,插入排序InsertSort.堆排序HeapSort.快速排序QuickSort.合并排序MergeSort,计数排序CountingSort,基数排序RadixSo

(最短路径算法整理)dijkstra、floyd、bellman-ford、spfa算法模板的整理与介绍

这一篇博客以一些OJ上的题目为载体.整理一下最短路径算法.会陆续的更新... 一.多源最短路算法--floyd算法 floyd算法主要用于求随意两点间的最短路径.也成最短最短路径问题. 核心代码: /** *floyd算法 */ void floyd() { int i, j, k; for (k = 1; k <= n; ++k) {//遍历全部的中间点 for (i = 1; i <= n; ++i) {//遍历全部的起点 for (j = 1; j <= n; ++j) {//遍历

算法14---B树

算法14---B树 更详细的讲解见http://www.xuebuyuan.com/509072.html 一棵m阶的B 树 (m叉树)的特性,如下: (1)树中每个结点含有最多含有个孩子,即m满足:ceil(m/2)-1<=n<=m-1. (2)除根结点和叶子结点外,其它每个结点至少有[ceil(m / 2)]个孩子(其中ceil(x)是一个取上限的函数): (3)若根结点不是叶子结点,则至少有2个孩子(特殊情况:没有孩子的根结点,即根结点为叶子结点,整棵树只有一个根节点): 1.1.插入(