P1576 minimum cost solution to a problem

CSDN synchronization

Original title link

Pre-knowledge:

Shortest. \ (\ texttt {SPFA, dijkstra } \) will be a problem this can be solved.

Briefly meaning of the questions:

Several groups are known relationship \ (the X-, the y-, z \) , namely \ (x \) and \ (y \) They need to deduct transfer \ (z \% \) fee ( swallow money ), asks \ ( A \) to \ (B \) making money, at least how much fight in order to ensure \ (B \) to obtain \ (100 \) yuan.

This era swallow the money more and more people.

  • FIG built in: \ (X \) and \ (Y \) connected side, the right side of \ (l- \ FRAC} {100} {Z \) , represents \ (T \) dollars from flowing through the sides , on the left \ ((l- \ FRAC} {100} {Z) \ Times T \) , the remainder being swallowed .

  • Methods: find the shortest path algorithm using \ (A \) to \ (B \) maximum exchange rate.

  • Cumulative program: the exchange rate is multiplication than addition, and traditional shortest different.

  • Shortest: I use the \ (\ texttt SPFA} {\) (the card anyway. \ (O (the n-^ 2) \) I am not afraid, \ (the n-\ Leq 2000 \) is so weak)

Time complexity: \ (O (n-2 ^) \) . ( \ (\ Texttt the SPFA} {\) time complexity is so metaphysical)

Actual score: \ (100 pts \) .

#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;

const int N=2e3+1;

inline int read(){char ch=getchar();int f=1;while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
	int x=0;while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}

int n,m,s,t; bool vis[N];
vector<pair<int,double> >G[N];
double ans=1e9,dis[N];

inline double min(double a,double b) {
	return a<b?a:b;
}

inline void SPFA() {
	queue<int>q; q.push(s);
	dis[s]=1; vis[s]=1; //准备宽搜
	while(!q.empty()) {
		int u=q.front(); q.pop();
		vis[u]=0; //每个节点入队多次,所以
		for(int i=0;i<G[u].size();i++) { //枚举转移
			int v=G[u][i].first;
			if(dis[u]*G[u][i].second>dis[v]) {
				dis[v]=dis[u]*G[u][i].second; //把钱吞掉
				if(!vis[v]) { //维护哈希
					vis[v]=1; q.push(v);
				}
			}
		}
	}
}

int main(){
//	freopen("money.in","r",stdin);
//	freopen("money.out","w",stdout);
	n=read(),m=read();
//	for(int i=1;i<=n;i++) dis[i]=0;
	while(m--) {
		int x=read(),y=read();
		double z; cin>>z;
		G[y].push_back(make_pair(x,1-z/100));
		G[x].push_back(make_pair(y,1-z/100));
	} s=read(),t=read(); SPFA(); //建图,跑最短路
	cout<<fixed<<setprecision(8)<<100/dis[t]<<endl; 
	return 0;
}

Guess you like

Origin www.cnblogs.com/bifanwen/p/12631872.html