lightOJ 1019 - Brush (V)

挑战上面有模板
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>

using namespace std;

const int MAX_V = 105;
const int INF = 0x3f3f3f3f;
int V,E;
int d[MAX_V];
int used[MAX_V];
int G[MAX_V][MAX_V];

void dijkstra(int s)
{
  d[s] = 0;
  while (true)
  {
    int v = -1;
    for (int u = 1; u <= V; u++)
    {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = 1;
    for (int i = 1; i <= V; i++)
    {
      if (d[i] > d[v] + G[v][i])
        d[i] = d[v] + G[v][i];
    }
  }
}

int main()
{
  int T,cas = 0;
  scanf("%d",&T);
  while (T--)
  {
    cin >> V >> E;
    for (int i = 1; i <= V; i++)
    {
      d[i] = INF, used[i] = 0;
      for (int j = 1; j <= V; j++)
        G[i][j] = INF;
    }
    for (int i = 1; i <= E; i++)
    {
      int t1, t2, t3;
      cin >> t1 >> t2 >> t3;
      if(G[t1][t2] > t3)
        G[t1][t2] = G[t2][t1] = t3;
    }
    dijkstra(1);
    if(d[V] == INF)
      printf("Case %d: Impossible\n",++cas);
    else
      printf("Case %d: %d\n",++cas,d[V]);
  }
  return 0;
}

你可能感兴趣的:(C++,dijkstra)