行程
行程(bug.cpp/ bug.in/ bug.out)
注意事项: 本题时限 2 秒。
问题描述:
你的电子日历包含一个错误——就是我们俗称的 bug。因为这个 bug,偶数不能输入到日历里。
你正在计划从 Bytetown 出差去 Bitcity。很显然,你想要走最短的路程。在你回来之后,你要把这次的行程长度输入到日历里,所以长度必须是一个奇数。
考虑到 bug 会存在很长时间,而且 Bytetown 的道路系统很可能会重建很多次,你决定编写一个程序来帮助你解决将来可能遇到的类似问题。
编写程序解决:
读入 Byteland 的地图描述。计算并输出从 Bytetown 到 Bitcity的最短奇数长度路径或者 判断这样的路径是否存在。
问题输入:
输 入 文 件 第 一 行 包 含 两 个 被 空 格 分 开 的 整 数 n 和 m(2<=n<=200000,0<=m<=500000),分别表示城市的数量和道路的数量。城市从 1 到 n 编号,其中Bytetown 编号为 1, Bitcity编号为 n。
接下来 m 行描述道路系统。每行包含三个整数 a, b, c(1<=a,b<=N,a<>b, 1<=c<=1000)表示从城市 a 到城市 b 有一条长度为 c 的双向边。
问题输出:
输出文件包含一个整数——最短奇数路径长度。
计算出的路径可以访问每个城市和道路多次。只能在城市进行方向转变(包括调头)。
如果不存在这样的路径,输出 0。
输入样例:
6 7
1 2 1
2 6 1
1 3 1
5 6 1
3 5 2
3 4 1
5 4 4
输出样例:
7
看起来很简单的题结果我叒只拿了50分
屈老师说这题数据有问题 太大了
然后看数据 果然是最大的五个数据我WA+TLE了
所以我觉得我可能其实写对了的...
那就不改了吧
其实是因为懒到爆炸
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 const int MAX = 1e7 + 5 ; 5 6 int n , m , k ; 7 int next[1001000] , to[1001000] , head[210000] , cost[1001000]; 8 int f[210000][2] ; 9 10 int read(){ 11 char c ; 12 int sign = 1 ; 13 while((c = getchar())<'0'|| c >'9') 14 if(c=='-') sign = -1 ; 15 int ans = c - '0' ; 16 while((c = getchar())>='0'&&c<='9') 17 ans = ans*10 + c - '0' ; 18 return ans * sign ; 19 } 20 21 void add( int a , int b , int c){ 22 next[++ k] = head[a] , to[k] = b , head[a] = k ; 23 next[++ k] = head[b] , to[k] = a , head[b] = k ; 24 cost[k - 1] = cost[k] = c ; 25 } 26 27 void find( int a ){ 28 for(int i = head[a] ; i ; i = next[i]){ 29 int To = to[i] , Cost = cost[i] , sign = 0 ; 30 int A = f[a][0] + Cost , B = f[a][1] + Cost ; 31 if((A & 1) && A < f[To][1] ) { 32 f[To][1] = A ; 33 sign = 1 ; 34 } 35 if((! (A & 1 )) && A < f[To][0]) { 36 f[To][0] = A ; 37 sign = 1 ; 38 } 39 if((B & 1) && B < f[To][1]){ 40 f[To][1] = B ; 41 sign = 1 ; 42 } 43 if((! (B & 1 )) && B < f[To][0]) { 44 f[To][0] = B ; 45 sign = 1 ; 46 } 47 if(sign) find(To) ; 48 } 49 return ; 50 } 51 52 int main(){ 53 freopen("bug.in","r",stdin); 54 freopen("bug.out","w",stdout) ; 55 n = read() , m = read() ; 56 f[1][0] = 0 , f[1][1] = 0 ; 57 for(int i = 2 ; i <= n ; ++ i){ 58 f[i][0] = f[i][1] = MAX ; 59 } 60 for(int i = 1 ; i <= m ; ++ i){ 61 int a = read() , b = read() , c = read() ; 62 add( a , b , c ) ; 63 } 64 find(1) ; 65 if(f[n][1] == MAX) { 66 cout << 0 ; 67 return 0 ; 68 } 69 cout << f[n][1] <<endl ; 70 return 0 ; 71 }