【HDU 1599】 find the mincost route 最小環模板題

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.

模板題,直接套就行了。

AC代碼:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e2+5;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll d[maxn][maxn];
ll e[maxn][maxn];

int n,m;

void init(){
	rep(i,1,n)
	rep(j,1,n) if(i==j) d[i][j] = e[i][j] = 0; else d[i][j] = e[i][j] = inf;
}


int floyd(){
	int i;
	int j;
	int k;
	int mincircle = inf;

	for(k = 1 ; k <= n ; ++k){
		for(i = 1 ; i < k ; ++i){
			for(j = i+1 ; j < k ; ++j){
				if(d[i][j] + e[i][k] + e[k][j] < mincircle){
					mincircle = d[i][j] + e[i][k] + e[k][j];
				}
			}
		}

		for(i = 1 ; i <= n ; ++i){
			for(j = 1 ; j <= n ; ++j){
				if(d[i][j] > d[i][k] + d[k][j]){
					d[i][j] = d[i][k] + d[k][j];
				}
			}
		}
	}

	return mincircle;
}


int main(){
	while(scanf("%d%d",&n,&m)!=EOF){
		init();
		int i;
		for(i = 1 ; i <= m ; ++i){
			int a;
			int b;
			int c;
			scanf("%d%d%d",&a,&b,&c);
            e[a][b] = e[b][a] = d[a][b] = d[b][a] = min(e[a][b],c);
		}


		int ans = floyd();

		if(ans != inf){
			printf("%d\n",ans);
		}else{
			printf("It's impossible.\n");
		}
	}

	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章