1072. Gas Station

来源:互联网 发布:java微信推送模板消息 编辑:程序博客网 时间:2024/06/10 18:09
#include<stdio.h>#include<vector>#include<string>#include<cstdlib>#include<iostream>using namespace std;#define N 1012#define M 100001struct E{    int next;    int dis;};vector<E> edge[N];int dis[11][M];int sum[11];bool mark[11][N];bool chao[11];bool you[11];int str2int(char* str,int n){    int num;    if(str[0] == 'G'){        num = atoi(str + 1) + n;        you[num - n] = true;    }    else{        num = atoi(str);    }    return num;}int main(){    int m,n,k,r;    scanf("%d%d%d%d",&n,&m,&k,&r);    for(int i = 1;i <= n + m;i ++) edge[i].clear();    for(int i = 1;i <= 10;i ++) you[i] = false;    while(k --){        int x,y,dis;        char str1[20],str2[20];        scanf("%s%s%d",str1,str2,&dis);        x = str2int(str1,n);        y = str2int(str2,n);        E tmp;        tmp.next = y,        tmp.dis = dis;        edge[x].push_back(tmp);        tmp.next = x;        edge[y].push_back(tmp);    }    for(int j = 1;j <= 10;j ++){        chao[j] = false;        sum[j] = 0;    }    int gasmin[11];    for(int ii = 1;ii <= m;ii ++){        int gas = ii;        int newp = ii + n;        for(int i = 1;i <= m + n;i ++){            dis[gas][i] = -1;            mark[gas][i] = false;        }        dis[gas][newp] = 0;        mark[gas][newp] = true;        for(int i = 1;i < m + n;i ++){            for(int j = 0;j < edge[newp].size();j ++){                int t = edge[newp][j].next;                int d = edge[newp][j].dis;                if(mark[gas][t] == true) continue;                if(dis[gas][t] == -1 || dis[gas][t] > dis[gas][newp] + d){                    dis[gas][t] = dis[gas][newp] + d;                }            }            int min = 123432434;            for(int j = 1;j <= m + n;j ++){                if(mark[gas][j] == true || dis[gas][j] == -1) continue;                if(min > dis[gas][j]){                    min = dis[gas][j];                    newp = j;                }            }            mark[gas][newp] = true;        }        sum[gas] = gasmin[gas] = dis[gas][1];        if(dis[gas][1] > r){            chao[gas] = true;            break;        };        for(int i = 2;i <= n;i ++){            if(gasmin[gas] > dis[gas][i]){                gasmin[gas] = dis[gas][i];            }            if(dis[gas][i] > r){                chao[gas] = true;                break;            }            sum[gas] += dis[gas][i];        }    }    int ans = 1;    for(int i = 2;i <= m;i ++){        if(chao[i] == true || you[i] == false) continue;        if(gasmin[ans] < gasmin[i] || gasmin[ans] == gasmin[i] && sum[ans] > sum[i]){            ans = i;            continue;        }    }    if(chao[ans] == true || you[ans] == false ||gasmin[ans] == -1)        cout<<"No Solution"<<endl;    else{        cout<<"G"<<ans<<endl;        printf("%.1f %.1f\n",gasmin[ans]*1.0,sum[ans]*1.0/n);    }    return 0;}

0 0
原创粉丝点击