[USACO19DEC]Milk Pumping G (dijkstra)

题目链接:P5837
此题相对于普通的单源最短路径,新增加了一个限制,就是流量的限制,对于花费来说,总花费等于路径上花费之和,但是流量是等于路径上所有流量中最低的那个,如果数据量很大的话可以考虑二分,但是此题数据只有一千,所以可以从一到一千枚举最小流量,每次枚举都跑一遍dijkstra,时间复杂度大约是n方乘Log2n。
AC代码:

#include
using namespace std;
int dis[1005], m;
const int inf = 1e8 + 5;
struct node
{
    int to;
    int liu;
    int money;
    bool operator<(const node &a)const
    {
        return money > a.money;
    }
};
vectornumbers[1005];
int check[1005];
void dijkstra(int k)
{
    memset(check, 0, sizeof(check));
    for (int i = 1; i <= m; i++)dis[i] = inf;
    priority_queueans;
    node t = { 1,1005,0 };
    ans.push(t);
    dis[1] = 0;
    while (!ans.empty())
    {
        t = ans.top();
        ans.pop();
        if (check[t.to])continue;
        check[t.to] = 1;
        //cout << t.to << endl;
        for (int i = 0; i = k && dis[t.to] + numbers[t.to][i].money < dis[numbers[t.to][i].to])
            {
                dis[numbers[t.to][i].to] = dis[t.to] + numbers[t.to][i].money;
                node tt={numbers[t.to][i].to,numbers[t.to][i].liu,dis[numbers[t.to][i].to]};
                ans.push(tt);
            }
        }
    }
}
int main()
{
    int  n; cin >> m >> n;
    for (int i = 0; i < n; i++)
    {
        int a, b, c, d; cin >> a >> b >> c >> d;
        node t = { a,d,c }, tt = { b,d,c };
        numbers[a].push_back(tt);
        numbers[b].push_back(t);

    }
    int ANS = 0;
    for (int i = 1; i <= 1005; i++)
    {
        dijkstra(i);
        if(dis[m]!=inf)ANS = max(ANS, (int)((double)i *1e6/ (double)dis[m]));
        //cout<

你可能感兴趣的:(个人题解,dijkstra,算法)