1072. Gas Station (30)

http://www.patest.cn/contests/pat-a-practise/1072

读题啊读题!!!

思路简单,只需要用dij,然后按要求输出,但是就2点不过,不知道那个要求读错了sad。。

等过几天清醒了再重新看吧

新知识

将字符串转化为其他形式或将其他形式转化为字符串

sscanf:char[]转其他和sprintf:其他转char[]

//scanf是从屏幕到n,故sscanf也是从str到n
sscanf(str,"%d",&n);
scanf("%d",&n);
//printf是从n到屏幕,故sprintf也是从n到str
<pre name="code" class="cpp">sprintf(str,"%d",n);
printf("%d",n);

 


//代码可优化,dis用一位数字即可,只要记住最大最小就好
//输入num[3]时,scanf("%s %s %d")
#include <cstdio>
#include <vector>
#include <string.h>
#define  MAX 1100
using namespace std;
int dis[MAX][MAX];
struct node{
	int next;
	int wei;
};
vector <node> adj[MAX];
int n,m,k,ds;
int vis[MAX];
void dij(int x){
	memset(vis,0,sizeof(vis));
	int newp=x;
	vis[newp]=1;
	dis[x][newp]=0;
	for(int i=0;i<n-1;i++){
		for(int j=0;j<adj[newp].size();j++){
			int u=adj[newp][j].next;
			int c=adj[newp][j].wei;
			if(vis[u]==0 && dis[x][u]==-1 || dis[x][u]>dis[x][newp]+c){
				dis[x][u]=dis[x][newp]+c;
			}
		}
		int min=123123123;
		for(int j=0;j<MAX;j++){
			if(dis[x][j]!=-1 && vis[j]==0 && dis[x][j]<min){
				min=dis[x][j];
				newp=j;
			}
		}
		vis[newp]=1;
	}

}
//dij,无需记录路径,只要记录,每个gas的dis就好
int main(){
	memset(dis,-1,sizeof(dis));
	freopen("in.txt","r",stdin);
	scanf("%d %d %d %d",&n,&m,&k,&ds);
	getchar();
	for(int j=0;j<k;j++){
		char s[3][10];
		int num[3];
		for(int i=0;i<3;i++){
			scanf("%s",s[i]);
			getchar();
			if(i==2){
				//将字符串转化为数字
				sscanf(s[i],"%d",&num[i]);
			}else{
				if(s[i][0]=='G'){
					s[i][0]='0';
					sscanf(s[i],"%d",&num[i]);
					num[i]+=n;
				}else{
					sscanf(s[i],"%d",&num[i]);
				}
				
			}
		}
		node tmp;
		tmp.next=num[0];
		tmp.wei=num[2];
		adj[ num[1] ].push_back(tmp);
		tmp.next=num[1];
		adj[ num[0] ].push_back(tmp);
	}
	int total=1231213123,ans=-1,maxlu=0;
	int cnt=0;
	for(int i=n+1;i<=n+m;i++){
		dij(i);
		int all=0;
		int tmax=123123123;
		for(int j=1;j<=n;j++){
			if(dis[i][j]>ds){
				cnt++;
				break;
			}else{
				all+=dis[i][j];
				if(dis[i][j]<tmax){
					tmax=dis[i][j];
				}
			}
		}
		if(maxlu<tmax){
			maxlu=tmax;
			total=all;
			ans=i;
		}else if(maxlu==tmax && total>all){
			total=all;
			ans=i;
		}else if(maxlu==tmax && total==all && i<ans){
			ans=i;
		}
	}
	if(cnt==m){
		printf("No Solution\n");
	}else{
		double a=(double)total/n;
		double b=(double)maxlu;
		printf("G%d\n",ans-n);
		printf("%.1lf %.1lf\n",b,a);
	}
	

	
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
最新发布
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值