HDU 1863畅通工程

来源:互联网 发布:sql语句里case when 编辑:程序博客网 时间:2024/05/21 15:39

刚开始,写错一个变量名,没有被声明的变量,结果编译器没有查出错,导致我一直结果不正确,还有就是在最后一定要把vector数组清空掉。

#include <iostream>#include <cstdio>#include <vector>#include <map>#include <queue>#include <cstring>using namespace std;const int MAX = 105;const int INF = 0x3f3f3f;int flag[MAX], min_cost[MAX];struct EDGE{    int to, cost;    EDGE(int a, int b)    : to(a), cost(b){}};vector<EDGE> g[MAX];typedef pair<int, int> P;int main(){    int n, m;    while (scanf("%d%d", &n, &m))    {        if (n == 0)            break;        memset(flag, 0, sizeof(flag));        memset(min_cost, INF, sizeof(min_cost));        int t_from, t_to, t_cost;        for (int i = 1; i <= n; ++i)        {            scanf("%d%d%d", &t_from, &t_to, &t_cost);            g[t_from].push_back(EDGE(t_to, t_cost));            g[t_to].push_back(EDGE(t_from, t_cost));        }        priority_queue<P, vector<P>, greater<P> > store;        store.push(P(0, 1));        min_cost[1] = 0;        int res = 0;       // printf("res = %d\n",res);        while (!store.empty())        {            P tmp = store.top();            store.pop();            int id = tmp.second;            int dis = tmp.first;          // printf("id = %d\n", id);            if (flag[id])                continue;            flag[id] = 1;           // printf("dis = %d\n", dis);            res += dis;          // printf("res = %d\n", res);            int lenth_g = g[id].size();            for (int i = 0; i < lenth_g; ++i)            {                EDGE t_e = g[id][i];               // printf("min_cost = %d\n", min_cost[t_e.to]);                if (min_cost[t_e.to] > t_e.cost)                {                    min_cost[t_e.to] = t_e.cost;                    //printf("t_e.cost = %d\n", t_e.cost);                    store.push(P(t_e.cost, t_e.to));                }            }           // printf("queue size =  %d\n", store.size());        }      //  cout << res << endl;        bool res_true = true;        for (int i = 1; i <= m; ++i)        {            if (!flag[i])            {                res_true = false;                break;            }        }        if (!res_true)        {            printf("?\n");        }        else        {            printf("%d\n", res);        }        for (int i = 1; i <= m; ++i)        {            g[i].clear();        }    }    return 0;}


0 0
原创粉丝点击