描述

Once,in a kingdom,there are N cities.M roads have been buit such that from one city you can reach any other cities.Between any two cities there is at most one road.But after a series war,D road are destoryed.The king wants to repair the road system.There are two important cities A and B,The king wants to make  the two cities connected as soon as possible.Now it is your job to repair some roads such that A and B are connected and the length of all roads repaired is minimun.

输入

Input may contain seveal test data sets.
For
each data set, the first line contain an integer
N(2<N<100),indicating the number of cities.The cities are numbered
from 1 To N.then the second line contains an integer M (
N-1<M<N*(N-1)/2),indicating the number of roads.Next come M
lines,each contains three integers I,J,K,which means that there is a
road between I and J,whose length is K.
 Then next line contains an
integer D(1<=D<=M),indicating the number of roads which are
destoryed.The following D lines each contain two integer I,J,which means
that the road which directly connected city I and J has been destoryed.
 The last line contains two integer A,B,indicating the two important cities.

Input is ended by N = 0,which should not be processed.

输出

For
each test case,just output one line with the total length of the roads
needed to repair such that A and B are connected.If we could not raech B
from A,output"-1".

样例输入

3
2
1 2 1
2 3 2
1
1 2
1 3
0

样例输出

1

每个城市之间都有一些路连接,某些城市之间的有遭到了破坏,现在要求我们求修路的最短长度。

如果修完所有的路也无法保证通行则输出-1。

逆向思维,让城市之间已经存在的路的长度为0,需要修补的初始化要修补的路的长度。这样就变成了简单的单源最短路径问题了。

#include <stdio.h>
#include <string.h>
#define MAXN 110
#define inf 0x3f3f3f3f int N;
int m1[MAXN][MAXN];
int m2[MAXN][MAXN];
int dist[MAXN];
int visited[MAXN]; void dijkstra(int begin){
int i,j,k;
k=begin;
for(j=1; j<=N; j++){
visited[j]=0;
if(j!=k){
dist[j]=m2[k][j];
}
}
visited[k]=1;
for(i=1;i<N-1;i++){
int min=inf;
k=0;
for(j=1; j<=N; j++){
if(!visited[j]&&dist[j]<min){
min=dist[j];
k=j;
}
}
if(k==0)break;
visited[k]=1;
for(j=1; j<=N; j++){
if(!visited[j]&& dist[k]+m2[k][j]<dist[j]){
dist[j]=dist[k]+m2[k][j];
}
}
}
} int main(){
int M,D,a,b,c;
int B,E;
while( scanf("%d",&N)!=EOF && N){
for( int i=1 ;i<=N; i++ ){
for( int j=1; j<=N; j++ ){
if(i==j)m2[i][j]=0;
else m2[i][j]=inf;
}
}
scanf("%d",&M);
for( int i=0; i<M; i++ ){
scanf("%d%d%d",&a,&b,&c);
m1[a][b]=c;
m1[b][a]=c;
m2[a][b]=0;
m2[b][a]=0;
}
scanf("%d",&D);
for( int i=0; i<D; i++ ){
scanf("%d%d",&a,&b);
m2[a][b]=m1[a][b];
m2[b][a]=m1[b][a];
}
scanf("%d%d",&B,&E);
dijkstra(B);
if(dist[E]==inf){
printf("-1\n");
}else{
printf("%d\n",dist[E]);
}
}
return 0;
}

TOJ 4394 Rebuild Road的更多相关文章

  1. POJ 3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  2. POJ3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  3. HDU 3080 The plan of city rebuild(除点最小生成树)

    题意  一个城市原来有l个村庄 e1条道路  又添加了n个村庄 e2条道路  后来后销毁了m个村庄  与m相连的道路也销毁了  求使全部未销毁村庄相互连通最小花费  不能连通输出what a pity ...

  4. HDU 3080 The plan of city rebuild(prim和kruskal)

    The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  5. Visual Studio 中 Build 和 Rebuild 的区别

    因为之前写的程序比较小,编译起来比较快,所以一直都没有太在意 Build 和 Rebuild 之间的区别,后来发现两个还是有很大不同. Build 只针对在上次编译之后更改过的文件进行编译,在项目比较 ...

  6. 解决 node-gyp rebuild 卡住 的问题

    node-gyp在编译前会首先尝试下载node的headers文件,像这样: gyp http GET https://nodejs.org/download/release/v6.8.1/node- ...

  7. AndroidStudio中make Project、clean Project、Rebuild Project的区别

    1.Make Project:编译Project下所有Module,一般是自上次编译后Project下有更新的文件,不生成apk. 2.Make Selected Modules:编译指定的Modul ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. Rebuild Instance 操作详解 - 每天5分钟玩转 OpenStack(37)

    上一节我们讨论了 snapshot,snapshot 的一个重要作用是对 instance 做备份. 如果 instance 损坏了,可以通过 snapshot 恢复,这个恢复的操作就是 Rebuil ...

随机推荐

  1. 组合(Composite)模式 *

    组合(Composite)模式:将对象组合树形结构以表示‘部分-整体’的层次结构. 组合模式使得用户对单个对象和组合对象具有一致性 /* * 抽象构件(Component)角色:这是一个抽象角色,它给 ...

  2. 图像中的掩膜(Mask)是什么

    在图像处理中,经常会碰到掩膜(Mask)这个词.那么这个词到底是什么意思呢?下面来简单解释一下. 1.什么是掩膜 首先我们从物理的角度来看看mask到底是什么过程. 在半导体制造中,许多芯片工艺步骤采 ...

  3. MultiTrigger

    MultiTrigger是多条件触发器.意为多个条件同时满足时才会触发. 用法和Trigger差不多. 但是MultiTrigger的条件是在写在自身的判断环境之中. 基本的语法是: <Mult ...

  4. 用python写个快排

    快排过程比较简单就直接上代码了: #!/usr/bin/python3 def quik_sort(L, left, right): if left <= right: key = L[left ...

  5. ionic中文教程[来自皓眸大前端]

    做前端的同学有福了,学完比较热火的angular,你就可以开始动手做静态的WebApp了,这是多么幸福的一件事啊.静态的WebApp,你可以做任何的Demo,甚至可以做一些通关小游戏这个先不谈.做完了 ...

  6. 【python】10分钟教你用python一行代码搞点大新闻

    准备 相信各位对python的语言简洁已经深有领会了.那么,今天就带大家一探究竟.看看一行python代码究竟能干些什么大新闻.赶紧抄起手中的家伙,跟我来试试吧. 首先你得先在命令行进入python. ...

  7. [集合]线程安全的HashMap

    一.一般模式下线程安全的HashMap 默认情况常用的HashMap都是线程不安全的,在多线程的环境下使用,常常会造成不可预知的,莫名其妙的错误.那么,我们如何实现一个线程安全的HashMap呢?其中 ...

  8. SpringCloud-Zuul搭建

    一.创建工程,在pom中引入Zuul 二.重写路由加载类,实在路由的动态注册和路由转发 package com.genius.gateway.zuul; import com.genius.gatew ...

  9. c#优秀文章

    文件传输示例]C# WinForm WebSocket (非浏览器):http://bbs.cskin.net/thread-4431-1-1.html NanUI for Winform发布,让Wi ...

  10. linux下使用文件IO监听GPIO中断

    完整的程序如下: #include<stdlib.h> #include<stdio.h> #include<string.h> #include<unist ...