【WA】题目1100:最短路径
来源:互联网 发布:js 多维数组排序 编辑:程序博客网 时间:2024/06/02 02:37
- 题目描述:
N个城市,标号从0到N-1,M条道路,第K条道路(K从0开始)的长度为2^K,求编号为0的城市到其他城市的最短距离
- 输入:
第一行两个正整数N(2<=N<=100)M(M<=500),表示有N个城市,M条道路
接下来M行两个整数,表示相连的两个城市的编号
- 输出:
N-1行,表示0号城市到其他城市的最短路,如果无法到达,输出-1,数值太大的以MOD 100000 的结果输出。
- 样例输入:
4 41 22 31 30 1
- 样例输出:
8911
代码:
#include <stdio.h>#include <vector>using namespace std;struct bigInteger { int digit[1000]; int size; void init() {for(int i=0;i<1000;i++) digit[i] = 0;size = 0; } void set(int x) {init();do { digit[size++] = x % 100000; x /= 100000;}while(x != 0); } bigInteger operator * (int x) {bigInteger ret;ret.init();int carry = 0;for(int i=0;i<size;i++) { int tmp = digit[i] * x + carry; carry = tmp / 100000; tmp = tmp % 100000; ret.digit[ret.size++] = tmp;}if(carry != 0) ret.digit[ret.size++] = carry;return ret; }}num;struct Edge { int next; int cost;}; vector<Edge> edge[501];int dis[101];bool mark[101];int main() { int n,m; while(scanf("%d %d",&n,&m) != EOF) {for(int i=0;i<m;i++) { int a,b; scanf("%d %d",&a,&b); Edge tmp; tmp.next = b; if(i == 0) {num.init();num.set(1); } else num = num * 2; tmp.cost = num.digit[0]; edge[a].push_back(tmp); tmp.next = b; edge[b].push_back(tmp);}for(int i=0;i<n;i++) { mark[i] = false; dis[i] = -1;}int newP = 0;mark[0] = true;dis[0] = 0;for(int i=1;i<n;i++) { for(int j=0;j<edge[newP].size();j++) {int next = edge[newP][j].next;int cost = edge[newP][j].cost;if(mark[next] == true) continue;if(dis[next] == -1 || dis[next] > (dis[newP]+cost) % 100000) dis[next] = (dis[newP] + cost) % 100000; } int min = 123123123; for(int j=0;j<n;j++) {if(mark[j] == true) continue;if(dis[j] == -1) continue;if(min > dis[j]) { min = dis[j]; newP = j;} } mark[newP] = true;}for(int i=1;i<n;i++) { if(mark[i] == true)printf("%d\n",dis[i]); elseprintf("-1\n");} } return 0;}
高精度整数结合最小路径。
利用(a+b) % c = (a % c + b % c) % c
直接就先把每个边的cost求模,存储。(不知道这样对吗?)
用题目提供的案例,测试正确。提交了一直WA。
- 【WA】题目1100:最短路径
- 题目1100:最短路径
- 题目1100:最短路径
- 题目1100:最短路径
- 题目1100:最短路径
- 题目1100:最短路径
- 九度 oj 题目1100:最短路径
- poj 1847 最短路径基础题目
- 题目77:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- 题目1008:最短路径问题
- Jobdu 题目1008:最短路径问题
- 题目1008:最短路径问题
- python使用socket实例
- 打印一个菱形(C语言)
- iPhone开发关于UDID和UUID的一些理解
- 成长和思考
- ORACLE更改字符码
- 【WA】题目1100:最短路径
- cuda i am coming!
- ActionContext和ServletActionContext小结
- Mysql 出错,Can't create/write to file C:\WINDOWS\TEMP\#sql_1588_0.MYI(Errcode:13)的解决办法
- nyoj 32 组合数
- 可重入 (Reentrancy) 和 线程安全 (Thread-Safe)
- 组合数(方法2)由大到小输出
- WIN7 IIS Default Web Site 无法启动,提示另一个程序正在使用,异常来自HRESULT:0x80070020
- 读书我会一如既往!