zoukankan      html  css  js  c++  java
  • JOJ 1343 城际公路网

    WA

    1. 函数体内用的是从 1 开始的数组, 而接受的 input 是从 0 开始的, 又是好2的错误

    代码

    #include <iostream>
    #include <stdio.h>
    #include <memory.h>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <set>
    #include <string>
    #include <deque>
    #include <cstring>
    #define MIN(x,y) (x)<(y)?(x):(y)
    using namespace std;
    
    int matrix[100][100];
    
    int totalCost(int n) {
    	int sum = 0;
    
    	for(int i = 1; i <= n; i ++)  {
    		for(int j = 1; j <= n; j ++)  {
    			sum += matrix[i][j];
    		}
    	}
    	return sum/2;
    }
    
    int updateMatrix(int n, int from, int to, int newCost)  {
    	matrix[from][to] = newCost;
    	matrix[to][from] = newCost;
    
    	for(int i = 1; i <= n; i ++)  {
    		for(int j = 1; j <= n; j ++)  {
    			int dist1 = matrix[i][from] + matrix[to][j] + newCost;
    			int dist2 = matrix[i][to] + matrix[from][j] + newCost;
    			matrix[i][j] = min(matrix[i][j], dist1);
    			matrix[i][j] = min(matrix[i][j], dist2);
    
    			printf("matrix[%d][%d] = %d
    ",i, j, matrix[i][j]);
    		}
    	}
    	return totalCost(n);
    }
    
    int main() {
    	freopen("C:\Users\vincent\Dropbox\workplacce\joj\test.txt", "r", stdin);
    	
    	int n;
    	while(scanf("%d", &n) != EOF)  {
    		for(int i = 1; i <= n; i ++)  {
    			for(int j = 1; j <= n; j ++)  {
    				scanf("%d", &matrix[i][j]);
    			}
    		}
    		int m;
    		scanf("%d", &m);
    		for(int i = 0; i < m; i ++)  {
    			int from, to, cost;
    			scanf("%d%d%d", &from, &to, &cost);
    			int res = updateMatrix(n, from, to, cost);
    			printf("%d
    ", res);
    		}
    
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    TZOJ 4085 Drainage Ditches(最大流)
    [国家集训队2]Tree I
    [USACO17DEC]Push a Box
    专题总结(图论)
    专题总结(分块)
    [noi.ac_D1T2]sort
    [Poi2004]SZN
    [HEOI2016/TJOI2016]求和
    [CF528D]Fuzzy Search
    [bzoj5093]图的价值
  • 原文地址:https://www.cnblogs.com/zhouzhuo/p/3679294.html
Copyright © 2011-2022 走看看