hdu2066一个人的旅行

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int INF = 0x7fffffff;
const int maxn = 1100;
struct Edge {
    int from, to, dist;
};
//
struct HeapNode {
    int d, u;
    bool operator < (const HeapNode& rhs)const {
        return d > rhs.d;
    }
};

int n, m;//point, edge
vector edges;
vector G[maxn];
bool done[maxn];
int d[maxn];
//int p[maxn];
int term[maxn];

void init() {
   for(int i = 0; i < maxn; i++) G[i].clear();//节点从1开始编号。
   edges.clear();
  // memset(p, 0, sizeof(p));
}

void AddEdge(int from, int to, int dist) {//注意是无向图,WA了好多次。
    edges.push_back((Edge){from, to, dist});
    m = edges.size();//edges
    G[from].push_back(m-1);//给每个边编号从0开始。
}

void Dijkstra()
{
    priority_queue Q;
    for(int i = 0; i <= maxn; i++){ //n,节点的编号并非为n
        d[i] = INF;
    }
    d[0] = 0;//用节点作为超级源点。
    memset(done, 0, sizeof(done));
    Q.push((HeapNode){0, 0});//d, u
    while(!Q.empty()) {
        HeapNode x = Q.top(); Q.pop();
        int u = x.u;
        if(done[u]) continue;
        done[u] = true;//标记已一个。经访问过且为解中的顶点
        for(int i = 0; i < (int)G[u].size(); i++) {
            Edge& e = edges[G[u][i]];
            if(d[e.to] > d[u] + e.dist) {
                d[e.to] = d[u] + e.dist;
               // p[e.to] = G[u][i]; //记录前驱节点编号。
                Q.push((HeapNode){d[e.to], e.to});
            }
        }
    }
}

int main()
{
    int T, S, D;
    n = maxn;
    while(scanf("%d%d%d", &T, &S, &D) != EOF) {//start, destination
        init();
        for(int i = 0; i < T; i++) {
            int from, to, dist;
            scanf("%d%d%d", &from, &to, &dist);
            AddEdge(from, to, dist);
            AddEdge(to, from, dist);
        }
        for(int i = 0; i < S; i++) {
            int to;
            scanf("%d", &to);
            AddEdge(0, to, 0);
        }
        for(int i = 0; i < D; i++) {
            scanf("%d", &term[i]);
        }
        Dijkstra();
        int res = INF;
        for(int i = 0; i < D; i++) {
            if(d[term[i]]

你可能感兴趣的:(ACM_图论理论及实现)