HDU 1863 畅通工程

刚开始,写错一个变量名,没有被声明的变量,结果编译器没有查出错,导致我一直结果不正确,还有就是在最后一定要把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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值