1. 程式人生 > >【杭電oj1599】find the mincost route無向圖最小環

【杭電oj1599】find the mincost route無向圖最小環

           find the mincost route

Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4335    Accepted Submission(s): 1738


Problem Description 杭州有N個景區,景區之間有一些雙向的路來連線,現在8600想找一條旅遊路線,這個路線從A點出發並且最後回到A點,假設經過的路線為V1,V2,....VK,V1,那麼必須滿足K>2,就是說至除了出發點以外至少要經過2個其他不同的景區,而且不能重複經過同一個景區。現在8600需要你幫他找一條這樣的路線,並且花費越少越好。

Input 第一行是2個整數N和M(N <= 100, M <= 1000),代表景區的個數和道路的條數。
接下來的M行裡,每行包括3個整數a,b,c.代表a和b之間有一條通路,並且需要花費c元(c <= 100)。
Output 對於每個測試例項,如果能找到這樣一條路線的話,輸出花費的最小值。如果找不到的話,輸出"It's impossible.".
Sample Input 3 3 1 2 1 2 3 1 1 3 1 3 3 1 2 1 1 2 3 2 3 1
Sample Output 3 It's impossible.
Author 8600
Source
Recommend 8600   |   We have carefully selected several similar problems for you:  
1595
 1598 1142 1217 1597  floyd演算法還有這用法沒想到,在下floyd演算法還沒搞清楚,該談什麼無向圖最小環,先記一下模板吧!
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#define INF 0xfffffff
using namespace std;
const int N = 1010;
int minn,n,m;
int map[N][N],dis[N][N];
void find() {
	int i,j,k;
	for(k=1; k<=n; k++) {
		for(i=1; i<k; i++) {
			for(j=i+1; j<k; j++) {
				minn=min(minn,map[i][k]+map[k][j]+dis[i][j]);
			}
		}
		for(i=1; i<=n; i++) {
			for(j=1; j<=n; j++) {
				dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
			}
		}
	}
}
int main() {
	while(scanf("%d%d",&n,&m)!=EOF) {
		for(int i=0; i<=n; i++) {
			for(int j=0; j<=n; j++) {
				map[i][j]=dis[i][j]=i==j?0:INF;
			}
		}
		for(int i=0; i<m; i++) {
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			if(map[a][b]>c)
				map[a][b]=map[b][a]=dis[a][b]=dis[b][a]=c;
		}
		minn=INF;
		find();
		if(minn>=INF)
			printf("It's impossible.\n");
		else
			printf("%d\n",minn);
	}
	return 0;
}

題目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1599

相關推薦

oj1599find the mincost route

           find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s

hdu 1599 find the mincost route 求從一個點遍歷所有節點以後回到原點的

                在寫題解之前給自己打一下廣告哈~。。抱歉了,希望大家多多支援我在CSDN的視訊課程,地址如下:http://edu.csdn.net/course/detail/209題目:find the mincost routeTime Limit: 1000/2000 MS (Java

HDU-1599-find the mincost route短路

find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4341 

USACO4.1.3籬笆迴路

題目意思就是讓你求無向圖最小環,但是給資料的方式非常噁心。 我的用並查集+暴力的方式…… 先給每個邊的頂點標號,然後……  把A能到B,B也能到A的邊的點,給併為一個點…… 然後floyd求最小環。 floyd最小環我自己還不是非常理解…… 但是先用著,上課再想

CodeChefAnnual Parade -費用大流&鏈覆蓋

傳送門:Annual Parade 題解 求鏈覆蓋所有點的最小花費,考慮拆點跑最小費用最大流。 1 0

-1599 find the mincost route

find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4164   

hdu 1599 find the mincost routefloyd求

find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3758   

OJ2032 楊輝三角

Problem Description 還記得中學時候學過的楊輝三角嗎?具體的定義這裡不再描述,你可以參考以下的圖形: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5

-oj-2008-數值統計

Problem Description 統計給定的n個數中,負數、零和正數的個數。 Input 輸入資料有多組,每組佔一行,每行的第一個數是整數n(n<100),表示需要統計的數值的

-oj-2076-夾角有多大

Problem Description 時間過的好快,一個學期就這麼的過去了,xhd在傻傻的看著表,出於對資料的渴望,突然他想知道這個表的時針和分針的夾角是多少。現在xhd知道的只有時間,請你幫

-oj-1008-Elevator(電梯)

Problem Description The highest building in our city has only one elevator. A request list is made up with N positive numbers. The num

-oj-1865-1sting(大數斐波那契數列)

1sting Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5021    Accepted Submiss

1285確定比賽名次

                                                  確定比賽名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe

oj2021發工資咯:)

發工資咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 51230    Accepted Submis

1878歐拉回路

歐拉回路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descr

-oj -1060-Leftmost Digit(輸出n的n次方左邊數)

Problem Description Given a positive integer N, you should output the leftmost digit of N^N. Input The input contains several test

-oj-2012-素數判定

Problem Description 對於表示式n^2+n+41,當n在(x,y)範圍內取整數值時(包括x,y)(-39<=x<y<=50),判定該表示式的值是否都為素數。

oj1159Common Subsequence

                             Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total

2035人見人愛A^B

#include<stdio.h> __int64 quick(int a,int b) { __int64 ans=1,base=a; __int64 t=0;

oj2546

飯卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20176    Accepted Submissio