• POJ 3613


     1 //共T条路,求从S到E经过N条路径的最短路径 
     2 #include <iostream>
     3 #include <fstream>
     4 #include <string.h>
     5 #include <algorithm>
     6 using namespace std;
     7 
     8 #define CIR(n, m) for (int i=0; i<n; i++) for (int j=0; j<m; j++)
     9 const int INF = 0x3fffffff;//少加个3就wa 
    10 
    11 struct Node
    12 {
    13     Node()
    14     {
    15           CIR(301,301) a[i][j]=INF;
    16     }
    17     int a[301][301];
    18     int* operator [](int x)//貌似运算符重载,可惜不懂啊  
    19     {
    20           return a[x];
    21     }
    22 };
    23 
    24 int N, T, S, E;
    25 int f[10001] = {0}, top=0;
    26 Node map;
    27 
    28 void floyd(Node &a, Node &b, Node &c)
    29 {
    30     Node temp;
    31     for (int k=1; k<=top; k++)
    32         for (int i=1; i<=top; i++)
    33             for (int j=1; j<=top; j++)
    34                 temp[i][j] = min(temp[i][j], a[i][k]+b[k][j]);
    35     CIR(301,301) c[i][j] = temp[i][j];
    36 }
    37 
    38 int find(int num)
    39 {
    40     Node ans;
    41     for (int i=0; i<301; i++) ans[i][i]=0;
    42     while (num)
    43     {
    44         if (num&1) 
    45           floyd(map, ans, ans);
    46         floyd(map, map, map);
    47         num >>= 1;
    48     }
    49     return ans[f[S]][f[E]];
    50 }
    51 
    52 int main()
    53 {
    54     int i,j,k;
    55     int length, from, to;
    56     cin>>N>>T>>S>>E;
    57     for (i=0; i<T; i++)
    58     {
    59         cin>>length>>from>>to;
    60         if (!f[from]) f[from]=++top;
    61         if (!f[to]) f[to]=++top;
    62         map[f[from]][f[to]] = map[f[to]][f[from]] = length;
    63     }  
    64     cout<<find(N)<<endl;
    65     return 0;
    66 }


    不懂啊     不懂啊

  • 相关阅读:
    3、Java基础类
    2、面向对象
    1、Java基础
    0.Eclipse
    【Python】UI自动化-1
    【Python】爬虫-2
    【Python】爬虫-1
    【Python】socket编程-3
    【Python】socket编程-2
    【Python】socket编程-1
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2663684.html
Copyright © 2020-2023  润新知