POJ 3411 Paid Roads(搜尋的小技巧)
題意:給你n個點,m條邊。每條邊裡面有a, b, c, r, p;代表從a到b如果c點經過了的話。那就要花費p元,否則花費r元。
需要注意的是:可能有環,所有每個點經歷的次數會大於1次。所以要加次數的上限。
用到了優先佇列,這樣保證第一次找到就是最短的。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4985 | Accepted: 1731 |
Description
A network of m roads connects N cities (numbered from 1 to N). There may be more than one road connecting one city with another. Some of the roads are paid. There are two ways to pay for travel on a paid road i from city ai to city bi:
- in advance, in a city ci (which may or may not be the same as ai);
- after the travel, in the city bi.
The payment is Pi in the first case and Ri in the second case.
Write a program to find a minimal-cost route from the city 1 to the city N.
Input
The first line of the input contains the values of N and m. Each of the following m lines describes one road by specifying the values of ai, bi, ci, Pi, Ri (1 ≤ i ≤ m). Adjacent values on the same line are separated by one or more spaces. All values are integers, 1 ≤ m, N ≤ 10, 0 ≤ Pi , Ri ≤ 100, Pi ≤ Ri (1 ≤ i ≤ m).
Output
The first and only line of the file must contain the minimal possible cost of a trip from the city 1 to the city N. If the trip is not possible for any reason, the line must contain the word ‘impossible’.
Sample Input
4 5 1 2 1 10 10 2 3 1 30 50 3 4 3 80 80 2 1 2 10 10 1 3 2 10 50
Sample Output
110
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-7
#define M 1000100
#define LL __int64
//#define LL long long
#define INF 0x3fffffff
#define PI 3.1415926535898
using namespace std;
const int maxn = 15;
int head[maxn];
int n, m, ans;
int num[maxn];
struct node1
{
int b, c, p, r;
int next;
} f[maxn];
struct node
{
int fa, sum;
bool vis[maxn];
bool operator < (const node &a) const
{
return a.sum < sum;
}
};
void add(int a, int b, int c, int p, int r)
{
f[ans].b = b;
f[ans].c = c;
f[ans].p = p;
f[ans].r = r;
f[ans].next = head[a];
head[a] = ans++;
}
void bfs()
{
struct node temp, xtemp;
memset(temp.vis, false, sizeof(temp.vis));
memset(num, 0 , sizeof(num));
temp.sum = 0;
temp.fa = 1;
num[1]++;
temp.vis[1] = true;
priority_queue<node>que;
que.push(temp);
while(!que.empty())
{
temp = que.top();
que.pop();
if(temp.fa == n)
break;
if(num[temp.fa] > 24)
continue;
int p = head[temp.fa];
while(p != -1)
{
xtemp = temp;
xtemp.vis[f[p].b] = true;
if(!xtemp.vis[f[p].c])
xtemp.sum += f[p].r;
else
xtemp.sum += f[p].p;
xtemp.fa = f[p].b;
num[f[p].b]++;
que.push(xtemp);
p = f[p].next;
}
}
if(temp.fa != n)
cout<<"impossible"<<endl;
else
cout<<temp.sum<<endl;
}
int main()
{
while(cin >>n>>m)
{
int a, b, c, p, r;
memset(head, -1, sizeof(head));
ans = 0;
for(int i = 0; i < m; i++)
{
cin >>a>>b>>c>>p>>r;
add(a, b, c, p, r);
}
bfs();
}
return 0;
}
相關文章
- github搜尋小技巧Github
- GitHub 搜尋專案小技巧Github
- Google搜尋技巧Go
- 【簡單搜尋】POJ 2251 Dugeon MasterAST
- POJ 1691 Painting A Board(dfs搜尋)AI
- Mac小技巧|Mac上如何搜尋隱藏檔案?Mac
- 使用Google百度等搜尋引擎的常用搜尋技巧Go
- Github高階搜尋技巧Github
- POJ 1724 ROADS(優先佇列+spfa)佇列
- 【樹形dp】poj 1947 Rebuilding RoadsRebuild
- 使用開源搜尋引擎 YaCy 的技巧
- POJ 1947 Rebuilding Roads(基礎的樹形dp)Rebuild
- GitHub 倉庫專案搜尋技巧Github
- 如何使用GOOGLE高階搜尋技巧Go
- 恕我直言,你可能連 GitHub 搜尋都不會用 - 如何精準搜尋的神仙技巧Github
- 使用 Google 高階搜尋的一些技巧Go
- VIM 搜尋命令使用方法和技巧
- win10搜尋框太長如何縮小_win10縮小搜尋框的步驟Win10
- 小程式搜尋,風暴之眼
- 谷歌自帶的線上工具和常用搜尋技巧谷歌
- POJ 1579-Function Run Fun(記憶化搜尋-遞迴)Function遞迴
- google搜尋運算子+101個Google技巧 - Google技巧的終極收集Go
- 【譯】20個更有效地使用谷歌搜尋的技巧谷歌
- 程式設計師應該掌握的10個搜尋技巧程式設計師
- BFS廣度優先搜尋(6)--poj3414(基礎題)
- windows10系統將小娜搜尋的方式換成百度搜尋的方法Windows
- 好的搜尋技巧能夠造就好的程式設計師程式設計師
- Win10技巧:Cortana搜尋怎麼解決Win10
- Win10正式版系統下將小娜搜尋換成谷歌搜尋的方法Win10谷歌
- Elasticsearch(ES)的高階搜尋(DSL搜尋)(上篇)Elasticsearch
- Elasticsearch(ES)的高階搜尋(DSL搜尋)(下篇)Elasticsearch
- 海量資料搜尋---搜尋引擎
- 助你掌握搜尋神器,10個實用的Elasticsearch查詢技巧Elasticsearch
- Mac新手需要了解的Spotlight搜尋N多個技巧Mac
- 微信小程式 簡易搜尋功能實現微信小程式
- Nebula 基於 ElasticSearch 的全文搜尋引擎的文字搜尋Elasticsearch
- 這些 Google 高階搜尋技巧,你都知道麼?Go
- 爬蟲的小技巧之–如何尋找爬蟲入口爬蟲