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
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- hdu 1863 畅通工程
- HDU-畅通工程-1863
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- HDU 1863 畅通工程
- hdu 1863 畅通工程
- Android中Fragment和Activity之间的通信(含接口回调)
- ubuntu设置SSH安全登录
- Java关于父类引用指向子类对象
- OSI与TCP/IP各层的结构与功能,都有哪些协议
- spring学习笔记(18)使用JNDI模拟访问应用服务器多数据源实例
- HDU 1863畅通工程
- Struts2中的类型转换器学习
- 单点登录SSO的实现原理
- Burst Balloons
- 多线程学习
- 关于Markdown编辑器的详细使用
- lua_c++
- 什么是C++虚函数、虚函数的作用和使用方法
- iOS 对于block的理解